Skip to content

Commit

Permalink
Add function to retrieve s3 object size
Browse files Browse the repository at this point in the history
  • Loading branch information
mpartio committed Apr 29, 2024
1 parent a59c4dd commit ec49e63
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions himan-lib/include/s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions himan-lib/source/s3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<long unsigned int*>(callbackData);

if (properties->contentLength)
{
*objectSize = properties->contentLength;
}

return S3StatusOK;
}

void Initialize()
{
call_once(
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit ec49e63

Please sign in to comment.