-
I refer to the document about STS code and S3 code separately and combine them into one.. However , it doesn't work . Just use official demo code and run in windows 10 VS 2019. code is #include <aws/s3/S3Client.h>
#include <aws/sts/STSClient.h>
#include <aws/sts/model/AssumeRoleRequest.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/model/ListBucketsResult.h>
#include <iostream>
#include "sts_samples.h"
#include <aws/core/Aws.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
#include "s3_examples.h"
bool AwsDoc::STS::assumeRole(const Aws::String &roleArn,
const Aws::String &roleSessionName,
Aws::Auth::AWSCredentials &credentials,
const Aws::Client::ClientConfiguration &clientConfig) {
Aws::STS::STSClient sts(clientConfig);
Aws::STS::Model::AssumeRoleRequest sts_req;
sts_req.SetRoleArn(roleArn);
sts_req.SetRoleSessionName(roleSessionName);
const Aws::STS::Model::AssumeRoleOutcome outcome = sts.AssumeRole(sts_req);
if (!outcome.IsSuccess()) {
std::cerr << "Error assuming IAM role. " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
std::cout << "Credentials successfully retrieved." << std::endl;
const Aws::STS::Model::AssumeRoleResult result = outcome.GetResult();
const Aws::STS::Model::Credentials &temp_credentials = result.GetCredentials();
// Store temporary credentials in return argument.
// Note: The credentials object returned by assumeRole differs
// from the AWSCredentials object used in most situations.
credentials.SetAWSAccessKeyId(temp_credentials.GetAccessKeyId());
credentials.SetAWSSecretKey(temp_credentials.GetSecretAccessKey());
credentials.SetSessionToken(temp_credentials.GetSessionToken());
}
return outcome.IsSuccess();
}
// snippet-end:[sts.cpp.assume_role]
bool AwsDoc::S3::ListObjects(const Aws::String &bucketName,
Aws::S3::S3Client s3_client) {
Aws::S3::Model::ListObjectsRequest request;
request.WithBucket(bucketName);
request.SetPrefix("xxxxxxxxxxxx");
auto outcome = s3_client.ListObjects(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error: ListObjects: " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
Aws::Vector<Aws::S3::Model::Object> objects =
outcome.GetResult().GetContents();
for (Aws::S3::Model::Object &object: objects) {
std::cout << object.GetKey() << std::endl;
}
}
return outcome.IsSuccess();
}
int main(int argc, char **argv)
{
Aws::SDKOptions options;
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
Aws::InitAPI(options);
{
Aws::String roleArn = "arn:aws:iam::xxxxxxxxxx:role/xxxxxxxxxxxx";
Aws::String roleSessionName = "xxxxx";
Aws::Auth::AWSCredentials credentials;
Aws::Client::ClientConfiguration clientConfig;
if (!AwsDoc::STS::assumeRole(roleArn, roleSessionName,credentials, clientConfig))
{
return 1;
}
const Aws::String bucket_name = "xxxxxxxxxx";
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy signPayloads = Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::RequestDependent;
Aws::S3::S3Client s3(credentials);
// Aws::S3::S3Client s3(credentials, clientConfig, signPayloads, false);
AwsDoc::S3::ListObjects(bucket_name, s3);
}
Aws::ShutdownAPI(options);
return 0;
} Error is :
|
Beta Was this translation helpful? Give feedback.
Answered by
jmklix
Feb 6, 2024
Replies: 1 comment 2 replies
-
if need additional information, pls let me know .Thanks. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you run the sts assume role sample and successfully call
listbuckets
? If so it might be something with your permissions, but it's hard to tell without any logs.Marking this as answered because of no response, but please let me know if you are still having any problems with this