diff --git a/modules/ANSUtilities/ANSAWSS3.cpp b/modules/ANSUtilities/ANSAWSS3.cpp index 7672572..2ea36e8 100644 --- a/modules/ANSUtilities/ANSAWSS3.cpp +++ b/modules/ANSUtilities/ANSAWSS3.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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 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, diff --git a/modules/ANSUtilities/ANSUtilities.h b/modules/ANSUtilities/ANSUtilities.h index d00882e..033e8b8 100644 --- a/modules/ANSUtilities/ANSUtilities.h +++ b/modules/ANSUtilities/ANSUtilities.h @@ -282,6 +282,22 @@ namespace ANSCENTER { [[nodiscard]] bool UploadJpegImage(const std::string& bucketName, unsigned char* jpeg_string, int32 bufferLength, const std::string& fileName, std::string& uploadedFilePath); [[nodiscard]] bool DownloadFile(const std::string& bucketName, const std::string& objectName, const std::string& saveFilePath); + // Generate a temporary pre-signed URL (AWS Signature V4) for an + // object already stored in S3, so public users can access it + // without AWS credentials until it expires. + // bucketName, prefix, objectName: the SAME three values passed + // to UploadPrefixJpegImage when the object was uploaded. + // (objectName is what UploadPrefixJpegImage calls its + // `fileName` parameter — the last segment of the S3 key.) + // validDays: URL validity in days, 1..7 (AWS caps pre-signed + // URLs at 7 days). + // The object key is rebuilt as "/", so the URL + // points at exactly the key that was uploaded. This is a local + // signing operation: it performs no network I/O, but + // Connect()/SetAuthentication() must have been called so the + // credentials and endpoint are known. + [[nodiscard]] bool GeneratePresignedURL(const std::string& bucketName, const std::string& prefix, const std::string& objectName, int validDays, std::string& presignedURL); + }; } @@ -380,6 +396,8 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3 extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* objectName, const char* savedFilePath); +extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, LStrHandle presignedURL); + // C++ test APIs (use std::string& instead of LStrHandle) extern "C" ANSULT_API int UploadTextDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* textDataPath, std::string& uploadedFilePath); extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, std::string& uploadedFilePath); @@ -389,6 +407,7 @@ extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, std::string& uploadedFilePath); extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, std::string& uploadedFilePath); extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath); +extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, std::string& presignedURL); #endif diff --git a/modules/ANSUtilities/dllmain.cpp b/modules/ANSUtilities/dllmain.cpp index 0a8e3ce..80baf47 100644 --- a/modules/ANSUtilities/dllmain.cpp +++ b/modules/ANSUtilities/dllmain.cpp @@ -2104,6 +2104,56 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3 return -4; } } +extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, LStrHandle presignedURL) { + ANS_DBG("ANSAWS", "GeneratePresignedURL called: HandlePtr=%p, *Handle=%p, bucket=%s, prefix=%s, object=%s, validDays=%d", + (void*)Handle, + Handle ? (void*)*Handle : nullptr, + bucketName ? bucketName : "(null)", + prefix ? prefix : "(null)", + objectName ? objectName : "(null)", + validDays); + if (Handle == nullptr || *Handle == nullptr || + bucketName == nullptr || prefix == nullptr || + objectName == nullptr || presignedURL == nullptr) + { + ANS_DBG("ANSAWS", "GeneratePresignedURL: returning 0 (null/invalid arg)"); + return 0; + } + AWSHandleGuard guard(AcquireAWSHandle(*Handle)); + if (!guard) { + ANS_DBG("ANSAWS", "GeneratePresignedURL: returning -1 (handle %p not in registry)", (void*)*Handle); + return -1; + } + try { + std::string outURL; + if (guard.get()->GeneratePresignedURL(bucketName, prefix, objectName, validDays, outURL)) { + int size = (int)outURL.length(); + MgErr error = DSSetHandleSize(presignedURL, sizeof(int32) + size * sizeof(uChar)); + if (error == noErr) { + (*presignedURL)->cnt = size; + memcpy((*presignedURL)->str, outURL.c_str(), size); + } + ANS_DBG("ANSAWS", "GeneratePresignedURL OK (1): object=%s -> %s", + objectName ? objectName : "(null)", outURL.c_str()); + return 1; + } + else { + ANS_DBG("ANSAWS", "GeneratePresignedURL FAILED (-2): object=%s", + objectName ? objectName : "(null)"); + return -2; + } + } + catch (std::exception& e) { + ANS_DBG("ANSAWS", "GeneratePresignedURL EXCEPTION (-3): object=%s, what=%s", + objectName ? objectName : "(null)", e.what()); + return -3; + } + catch (...) { + ANS_DBG("ANSAWS", "GeneratePresignedURL EXCEPTION (-4): object=%s, unknown exception", + objectName ? objectName : "(null)"); + return -4; + } +} extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, LStrHandle uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 ) { return 0; @@ -2221,6 +2271,14 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAW return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0; } catch (...) { return 0; } } +extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, std::string& presignedURL) { + if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || objectName == nullptr) return 0; + AWSHandleGuard guard(AcquireAWSHandle(*Handle)); + if (!guard) return 0; + try { + return guard.get()->GeneratePresignedURL(bucketName, prefix, objectName, validDays, presignedURL) ? 1 : 0; + } catch (...) { return 0; } +} // ════════════════════════════════════════════════════════════════════════════ // V2 API — handle-by-value for LabVIEW (uint64_t instead of Handle**) @@ -2837,6 +2895,28 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVa catch (...) { return 0; } } +extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* objectName, int validDays, LStrHandle presignedURL) { + auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; + if (bucketName == nullptr || prefix == nullptr || objectName == nullptr || presignedURL == nullptr) return 0; + AWSHandleGuard guard(AcquireAWSHandle(_v2h)); + if (!guard) return 0; + try { + std::string outURL; + if (guard.get()->GeneratePresignedURL(bucketName, prefix, objectName, validDays, outURL)) { + int size = (int)outURL.length(); + MgErr error = DSSetHandleSize(presignedURL, sizeof(int32) + size * sizeof(uChar)); + if (error == noErr) { + (*presignedURL)->cnt = size; + memcpy((*presignedURL)->str, outURL.c_str(), size); + } + return 1; + } + else return 0; + } + catch (std::exception& e) { return 0; } + catch (...) { return 0; } +} + extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 || uploadedFilePath == nullptr) return 0; diff --git a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp index bfd2ab6..59f2601 100644 --- a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp +++ b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp @@ -390,8 +390,8 @@ int main() { //ANSLLMOllamaTest(); - std::cout << "===== MinIO (path-style) =====" << std::endl; - ANSFTechMinIOTest(); + //std::cout << "===== MinIO (path-style) =====" << std::endl; + //ANSFTechMinIOTest(); std::cout << "\n===== AWS S3 (virtual-host) =====" << std::endl; ANSAWSTest();