在命令行上,我可以运行此 AWS CLI 命令来获取本地计算机上使用的 AWS UserId
$ aws sts get-caller-identity
{
"UserId": "123456789:john.doe",
"Account": "123456789",
"Arn": "arn:aws:sts::123456789:federated-user/john.doe"
}
我现在需要从适用于 C++ 的 AWS 开发工具包获取相同的信息(特别是 UserId)。我一直在尝试像这样使用 aws/sts/STSClient.h
库
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/sts/STSClient.h>
#include <aws/sts/model/GetAccessKeyInfoRequest.h>
#include <aws/sts/model/GetCallerIdentityRequest.h>
#include <array>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
void test() {
Aws::STS::STSClient stsClient;
Aws::STS::Model::GetCallerIdentityRequest getCallerIdentityRequest;
Aws::STS::Model::GetCallerIdentityOutcome callerIdentityOutcome =
stsClient.GetCallerIdentity(getCallerIdentityRequest);
if (callerIdentityOutcome.IsSuccess())
{
Aws::STS::Model::GetCallerIdentityResult callerIdentityResult = callerIdentityOutcome.GetResult();
Aws::String const &userIdAWSString = callerIdentityResult.GetUserId();
std::string userId(userIdAWSString.c_str(), userIdAWSString.size());
std::cout << "userId: " << userId << std::endl;
}
else
{
std::cout << "callerIdentityOutcome: Failed to retrieve STS GetCallerIdentityRequest" << std::endl;
}
}
但是在调用 Aws::STS::STSClient stsClient; 时不断出现异常
external/aws/aws-cpp-sdk-core/source/config/AWSProfileConfigLoader.cpp:498: Aws::Config::Profile Aws::Config::GetCachedConfigProfile(const Aws::String &): Assertion `s_configManager' failed.
有人知道我做错了什么吗?
更新:
我什至可以使用 AWS 的 Python SDK 的 Boto3 库来做到这一点
>>> import boto3
>>> client = boto3.client('sts')
>>> response = client.get_caller_identity()
>>> print(response)
{u'Account': '123456789', u'UserId': '123456789:john.doe', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '80e471ba-384a-4b6a-a5b8-f754f16c3a23', 'HTTPHeaders': {'x-amzn-requestid': '80e471ba-384a-4b6a-a5b8-f754f16c3a23', 'date': 'Fri, 27 Dec 2019 17:13:27 GMT', 'content-length': '431', 'content-type': 'text/xml'}}, u'Arn': 'arn:aws:sts::123456789:federated-user/john.doe'}
为什么这在 C++ 中这么难做到?
最佳答案
您是否正确初始化了SDK?这是一个骨架:
#include <aws/core/Aws.h>
int main(int argc, char** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);
// make your SDK calls here
Aws::ShutdownAPI(options);
return 0;
}
关于c++ - STS 获取调用者身份 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503142/