From ec49e637bbcfaad560deeb439f004b4ca8f5c9bf Mon Sep 17 00:00:00 2001 From: Mikko Partio Date: Sat, 27 Apr 2024 11:42:17 +0300 Subject: [PATCH] Add function to retrieve s3 object size --- himan-lib/include/s3.h | 1 + himan-lib/source/s3.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/himan-lib/include/s3.h b/himan-lib/include/s3.h index 1d656131..c1cd5d9e 100644 --- a/himan-lib/include/s3.h +++ b/himan-lib/include/s3.h @@ -9,5 +9,6 @@ namespace s3 buffer ReadFile(const file_information& fileInformation); void WriteObject(const std::string& objectName, const himan::buffer& buff); bool Exists(const std::string& objectName); +long unsigned int ObjectSize(const std::string& objectName); } // namespace s3 } // namespace himan diff --git a/himan-lib/source/s3.cpp b/himan-lib/source/s3.cpp index c914fdf1..348b2f56 100644 --- a/himan-lib/source/s3.cpp +++ b/himan-lib/source/s3.cpp @@ -139,6 +139,18 @@ static S3Status getObjectDataCallback(int bufferSize, const char* buffer, void* return S3StatusOK; } +static S3Status headObjectPropertiesCallback(const S3ResponseProperties* properties, void* callbackData) +{ + auto objectSize = static_cast(callbackData); + + if (properties->contentLength) + { + *objectSize = properties->contentLength; + } + + return S3StatusOK; +} + void Initialize() { call_once( @@ -431,6 +443,38 @@ bool s3::Exists(const std::string& objectName) } } +long unsigned int s3::ObjectSize(const std::string& objectName) +{ + Initialize(); + + const auto host = StripProtocol(util::GetEnv("S3_HOSTNAME")); + + const auto bucketAndFileName = GetBucketAndFileName(objectName); + const auto bucket = bucketAndFileName[0]; + const auto key = bucketAndFileName[1]; + const auto region = ReadAWSRegionFromHostname(host); + + const auto bucketContext = GetBucketContext(host, bucket, region); + const int timeoutms = 5000; + + long unsigned int size = 0; + + S3ResponseHandler responseHandler = {&headObjectPropertiesCallback, &responseCompleteCallback}; + + S3_head_object(&bucketContext, key.c_str(), NULL, timeoutms, &responseHandler, &size); + + himan::logger logr("s3"); + + switch (statusG) + { + case S3StatusOK: + return size; + default: + HandleS3Error(logr, host, bucket, key, "Size"); + himan::Abort(); + } +} + #else buffer s3::ReadFile(const file_information& fileInformation) {