AWS SDK for C++ 中,需要使用 STS(Security Token Service)的假定角色,可以通过 AWS 的 SDK 的 AssumeRole API 实现。而在 AWS SDK for C++ 中,该 API 是在 AWSCoreClient 中实现的。
下面是一个使用 AWS SDK for C++ 的 AssumeRole API 的示例代码,其中使用了 STS,并且实现了刷新假定角色的时间:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace Aws;
using namespace Aws::Auth;
using namespace Aws::Client;
using namespace Aws::STS;
using namespace Aws::STS::Model;
using namespace Aws::Utils;
using namespace Aws::Utils::Logging;
namespace AwsExample {
/**
* A custom credentials provider that uses AWS STS to assume a role.
* After assuming the role, it caches the temporary credentials and the expiration time.
* The credentials are automatically refreshed before they expire.
*/
class STSCredentialsProvider : public AWSCredentialsProvider {
public:
STSCredentialsProvider(
const std::shared_ptr< STSClient >& client,
const Aws::String& roleToAssumeARN,
unsigned expireInSeconds,
Aws::Utils::RateLimiterInterface* rateLimiter)
: m