c++amazon-s3localstackaws-sdk-cpp

aws-sdk-cpp + Localstack basic S3 queries don't working


I'm trying to make some S3 queries to Localstack by using the AWS C++ SDK. I've followed the examples provided by AWS in C++ in order to, for instance, list my buckets, but without any luck.

I share the C++ class I've made to achieve this:

class AWSwrapper
{
public:

    // Class members
    Aws::SDKOptions m_sdkOptions;
    std::unique_ptr<Aws::Client::ClientConfiguration> m_spClientConfig;
    
    // Class constructor
    AWSwrapper() 
    {
        m_sdkOptions.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug;
        Aws::InitAPI(m_sdkOptions);

        m_spClientConfig = std::make_unique<Aws::Client::ClientConfiguration>();
        m_spClientConfig->endpointOverride = Aws::String("127.0.0.1:4566");
        m_spClientConfig->scheme = Aws::Http::Scheme::HTTP;
    }

    // Class destructor
    ~AWSwrapper()
    {
        Aws::ShutdownAPI(m_sdkOptions);
    }

    // List available buckets
    std::vector<std::string> listBuckets() 
    {
        Aws::S3::S3Client client {*m_spClientConfig};
        const auto outcome {client.ListBuckets()};

        if (outcome.IsSuccess()) 
        {
            std::vector<std::string> bucketsList;

            for (auto &&bucket: outcome.GetResult().GetBuckets()) 
            {
                bucketsList.push_back(bucket.GetName());
            }

            return bucketsList;
        }
        
        throw std::runtime_error {std::string("Couldn't list buckets: ") + outcome.GetError().GetMessage()};
    }
};

Calling listBuckets will return an empty vector:

AWSwrapper wrapper;
const auto bucketsList {wrapper.listBuckets()};

The Localstack output when running my program:

2023-05-30T07:55:20.889  INFO --- [   asgi_gw_0] localstack.request.http    : GET / => 200`

The Localstack output when calling awslocal s3api list-buckets:

2023-05-30T08:02:33.610  INFO --- [   asgi_gw_0] localstack.request.aws     : AWS s3.ListBuckets => 200
{
    "Buckets": [
        {
            "Name": "test-bucket",
            "CreationDate": "2023-05-30T07:04:23Z"
        }
    ],
    "Owner": {
        "DisplayName": "webfile",
        "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
    }
}

Finally, this is a piece of the AWS SDK log output that may be relevant:

[DEBUG] 2023-05-30 08:07:45.724 CURL [140534065837120] (Text)   Trying 169.254.169.254:80...

[DEBUG] 2023-05-30 08:07:45.776 CURL [140534065837120] (Text) connect to 169.254.169.254 port 80 failed: No route to host

[DEBUG] 2023-05-30 08:07:45.776 CURL [140534065837120] (Text) Failed to connect to 169.254.169.254 port 80 after 51 ms: No route to host

[DEBUG] 2023-05-30 08:07:45.776 CURL [140534065837120] (Text) Closing connection 0

[ERROR] 2023-05-30 08:07:45.776 CurlHttpClient [140534065837120] Curl returned error code 7 - Couldn't connect to server
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Destroy curl handle: 0x559dfbf6f380
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Created replacement handle and released to pool: 0x559dfbf6f380
[ERROR] 2023-05-30 08:07:45.776 EC2MetadataClient [140534065837120] Http request to retrieve credentials failed
[ERROR] 2023-05-30 08:07:45.776 EC2MetadataClient [140534065837120] Can not retrieve resource from http://169.254.169.254/latest/meta-data/iam/security-credentials
[INFO] 2023-05-30 08:07:45.776 Aws::Config::AWSProfileConfigLoaderBase [140534065837120] Failed to reload configuration.
[DEBUG] 2023-05-30 08:07:45.776 AWSClient [140534065837120] Request Successfully signed
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Attempting to acquire curl connection.
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] No current connections available in pool. Attempting to create new connections.
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] attempting to grow pool size by 2
[INFO] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Pool grown by 2
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Connection has been released. Continuing.
[DEBUG] 2023-05-30 08:07:45.776 CurlHandleContainer [140534065837120] Returning connection handle 0x559dfc0c8080
[DEBUG] 2023-05-30 08:07:45.776 CurlHttpClient [140534065837120] Obtained connection handle 0x559dfc0c8080
[DEBUG] 2023-05-30 08:07:45.776 CURL [140534065837120] (Text)   Trying 127.0.0.1:4566...

[DEBUG] 2023-05-30 08:07:45.777 CURL [140534065837120] (Text) Connected to 127.0.0.1 (127.0.0.1) port 4566 (#0)

[DEBUG] 2023-05-30 08:07:45.777 CURL [140534065837120] (HeaderOut) GET / HTTP/1.1
Host: 127.0.0.1:4566
Accept: */*
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: AAMAAABkAAQCAAAAAAIAAAAA
amz-sdk-invocation-id: 9BCC97C1-2735-45C3-8FD4-94DB00D0BF3E
amz-sdk-request: attempt=1
user-agent: aws-sdk-cpp/1.11.84 Linux/6.2.6-76060206-generic x86_64 GCC/11.3.0


[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (Text) Mark bundle as not supporting multiuse

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (HeaderIn) HTTP/1.1 101 

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (HeaderIn) date: Tue, 30 May 2023 08:07:45 GMT

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (HeaderIn) server: hypercorn-h11

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (HeaderIn) connection: upgrade

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (HeaderIn) upgrade: h2c

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (Text) Received 101

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (Text) Using HTTP2, server supports multiplexing

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (Text) Connection state changed (HTTP/2 confirmed)

[DEBUG] 2023-05-30 08:07:45.780 CURL [140534065837120] (Text) Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) HTTP/2 200 

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) content-type: text/plain; charset=utf-8

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) access-control-allow-origin: *

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) access-control-allow-methods: HEAD,GET,PUT,POST,DELETE,OPTIONS,PATCH

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) access-control-allow-headers: authorization,cache-control,content-length,content-md5,content-type,etag,location,x-amz-acl,x-amz-content-sha256,x-amz-date,x-amz-request-id,x-amz-security-token,x-amz-tagging,x-amz-target,x-amz-user-agent,x-amz-version-id,x-amzn-requestid,x-localstack-target,amz-sdk-invocation-id,amz-sdk-request

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) access-control-expose-headers: etag,x-amz-version-id

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) content-length: 0

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) date: Tue, 30 May 2023 08:07:45 GMT

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) server: hypercorn-h2

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (HeaderIn) 

[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (DataIn) 
[DEBUG] 2023-05-30 08:07:45.824 CURL [140534065837120] (Text) Connection #0 to host 127.0.0.1 left intact

[DEBUG] 2023-05-30 08:07:45.824 CurlHttpClient [140534065837120] Returned http response code 200
[DEBUG] 2023-05-30 08:07:45.824 CurlHttpClient [140534065837120] Returned content type text/plain; charset=utf-8
[DEBUG] 2023-05-30 08:07:45.824 CurlHttpClient [140534065837120] Releasing curl handle 0x559dfc0c8080
[DEBUG] 2023-05-30 08:07:45.824 CurlHandleContainer [140534065837120] Releasing curl handle 0x559dfc0c8080
[DEBUG] 2023-05-30 08:07:45.824 CurlHandleContainer [140534065837120] Notified waiting threads.

What am I missing here?

FYI: Localstack v2.0.2 and aws-sdk-cpp v1.11.84.


Solution

  • So I managed to solve this by adding a dummy credential file in ~/.aws/credentials with the following content:

    [default]
    aws_access_key_id=test
    aws_secret_access_key=test
    
    [admin]
    aws_access_key_id=test
    aws_secret_access_key=test