Search code examples
aws-sdk-cpp

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


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.


Solution

  • 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