Skip to content

Commit

Permalink
C++: Update hello_s3.cpp (awsdocs#5254)
Browse files Browse the repository at this point in the history
Update hello_s3.cpp

Adding explicit authentication check. S3 allows anonymous requests. Due to an issue with the c++ sdk, if a user is not authenticated this method will return an unhelpful ‘0 buckets’ instead of a helpful ‘authentication issue’ message.  Since this example is for new users, lets be more helpful.
  • Loading branch information
annapan-aws authored and shepazon committed Aug 14, 2023
1 parent 1386a64 commit a36fa9b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cpp/example_code/s3/hello_s3/hello_s3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <iostream>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
using namespace Aws;
using namespace Aws::Auth;

/*
* A "Hello S3" starter application which initializes an Amazon Simple Storage Service (Amazon S3) client
Expand All @@ -36,6 +39,13 @@ int main(int argc, char **argv) {
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";

// You don't normally have to test that you are authenticated. But the S3 service permits anonymous requests, thus the s3Client will return "success" and 0 buckets even if you are unauthenticated, which can be confusing to a new user.
auto provider = Aws::MakeShared<DefaultAWSCredentialsProviderChain>("alloc-tag");
auto creds = provider->GetAWSCredentials();
if (creds.IsEmpty()) {
std::cerr << "Failed authentication" << std::endl;
}

Aws::S3::S3Client s3Client(clientConfig);
auto outcome = s3Client.ListBuckets();
Expand Down

0 comments on commit a36fa9b

Please sign in to comment.