Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the AWS C++ SDK to parse an S3 URI for region, bucket and object key?

Tags:

aws-sdk-cpp

I have an S3 URI, and I need to extract the region, bucket and object key so that I can use the Aws::S3::Model::PutObjectRequest to upload an object.

Does the AWS C++ SDK provide a utility function for doing purpose? Should I use a regular expression?

I note that the AWS Java SDK provides AmazonS3URI, but I haven't found anything similar so far in the C++ SDK.

Many thanks.

like image 263
Boinst Avatar asked Oct 18 '25 05:10

Boinst


2 Answers

The AWS SDK C++ provides Aws::Crt::Io::Uri which is general but you can definitely use it with s3.

    const std::string s3uri("s3://mybucket/sample-file-name");
    Aws::Crt::Io::Uri uri(Aws::Crt::ByteCursor{s3uri.size(), (uint8_t*)s3uri.data()});
    std::cout << "Scheme:" << Aws::Utils::StringUtils::FromByteCursor(uri.GetScheme())
              << std::endl;
    std::cout << "Authority:"
              << Aws::Utils::StringUtils::FromByteCursor(uri.GetAuthority()) << std::endl;
    std::cout << "Path:" << Aws::Utils::StringUtils::FromByteCursor(uri.GetPath())
              << std::endl;

Results in

Scheme:s3
Authority:mybucket
Path:/sample-file-name

Two years late but I hope someone this helps

like image 192
Henrique Bucher Avatar answered Oct 20 '25 21:10

Henrique Bucher


I don't think C++ SDK supports it. But S3 URIs are just those - URIs. So any decent URI parser should be able to parse S3 URIs, your bucket name will be parsed as host and a file key will be parsed as path.

like image 31
Artem Vovsia Avatar answered Oct 20 '25 22:10

Artem Vovsia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!