Support Generate Presigned URL for S3

This commit is contained in:
2026-05-19 10:37:46 +10:00
parent 26f9b07ac1
commit 214f598e18
4 changed files with 213 additions and 2 deletions
+112
View File
@@ -4,6 +4,7 @@
#include <CkAuthGoogle.h>
#include <CkSocket.h>
#include <CkGlobal.h>
#include <CkHttp.h>
#include <CkMailMan.h>
#include <CkEmail.h>
#include <CkMht.h>
@@ -2601,6 +2602,117 @@ namespace ANSCENTER
return retryable ? AttemptResult::Transient : AttemptResult::Permanent;
});
}
// Generates a temporary pre-signed URL (AWS Signature V4) for an object
// already stored in S3. Public users can open the URL — without any AWS
// credentials — until it expires. SigV4 pre-signing is a purely local
// operation: it needs valid credentials and a known endpoint, but performs
// no network I/O, so it succeeds even when not connected.
bool ANSAWSS3::GeneratePresignedURL(const std::string& bucketName,
const std::string& prefix,
const std::string& objectName,
int validDays,
std::string& presignedURL) {
const std::string kOp = "ANSAWSS3::GeneratePresignedURL";
// Permanent failures — do NOT proceed.
if (!_isLicenseValid || !_isUnlockCodeValid) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : "Invalid unlock code",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL: invalid license/unlock code");
return false;
}
if (bucketName.empty() || objectName.empty()) {
_logger.LogError(kOp, "Bucket name or object name is empty", __FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL: empty bucket/object name");
return false;
}
// AWS SigV4 caps pre-signed URL validity at 7 days.
if (validDays < 1 || validDays > 7) {
_logger.LogError(kOp,
"validDays must be between 1 and 7 (got " + std::to_string(validDays) + ")",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL: validDays out of range (%d)", validDays);
return false;
}
// Snapshot the config/auth attributes under the config mutex.
std::string accessKey, secretKey, region, endpoint, service;
bool useHttps;
{
std::lock_guard<std::mutex> lock(_configMutex);
accessKey = _accessKey;
secretKey = _secretKey;
region = _bucketRegion;
endpoint = _fullAWSURL;
service = _serviceName;
useHttps = _bTls;
}
if (accessKey.empty() || secretKey.empty()) {
_logger.LogError(kOp,
"AWS credentials not set (call SetAuthentication first)",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL: missing credentials");
return false;
}
if (endpoint.empty() || region.empty() || service.empty()) {
_logger.LogError(kOp,
"S3 endpoint/region/service not configured (call Connect first)",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL: endpoint/region/service not configured");
return false;
}
// Build the object key the same way UploadPrefixJpegImage does, so the
// URL is generated for exactly the key that was uploaded.
std::string objectKey;
if (!prefix.empty()) {
std::string normalizedPrefix = prefix;
if (normalizedPrefix.front() == '/') {
normalizedPrefix = normalizedPrefix.substr(1);
}
if (!normalizedPrefix.empty() && normalizedPrefix.back() != '/') {
normalizedPrefix += '/';
}
objectKey = normalizedPrefix;
}
objectKey += objectName;
// Configure a CkHttp with the same credentials/endpoint the upload
// path uses so the signed host matches the stored object's host.
CkHttp http;
http.put_AwsAccessKey(accessKey.c_str());
http.put_AwsSecretKey(secretKey.c_str());
http.put_AwsRegion(region.c_str());
http.put_AwsEndpoint(endpoint.c_str());
const int numSecondsValid = validDays * 86400;
const char* url = http.s3_GenPresignedUrl("GET", useHttps,
bucketName.c_str(), objectKey.c_str(), numSecondsValid, service.c_str());
if (!http.get_LastMethodSuccess()) {
_logger.LogError(kOp,
std::string("Pre-signed URL generation failed: ") + http.lastErrorText(),
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL FAILED: key=%s err=%s",
objectKey.c_str(), http.lastErrorText());
return false;
}
presignedURL = (url != nullptr) ? url : "";
_logger.LogDebug(kOp,
"Generated pre-signed URL for '" + objectKey + "' valid " +
std::to_string(validDays) + " day(s): " + presignedURL,
__FILE__, __LINE__);
ANS_DBG("AWSS3", "GeneratePresignedURL OK: key=%s validDays=%d url=%s",
objectKey.c_str(), validDays, presignedURL.c_str());
return true;
}
// Downloads
bool ANSAWSS3::DownloadFile(const std::string& bucketName,
const std::string& objectName,