指标数据在 AWS SDK for C++ CloudWatch 示例中进入错误区域

Metric data goes to wrong region in AWS SDK for C++ CloudWatch example

"AWS SDK for C++ Documentation Examples" 自述文件说:

To build and run these examples, you'll need: ... AWS credentials, either configured in a local AWS credentials file or by setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/cpp

凭据文件:

$ cat ~/.aws/credentials
[default]
aws_access_key_id = ...
aws_secret_access_key = ...
output = text
region = us-west-2

AWS 客户端命令正常运行:

$ aws cloudwatch put-metric-data \
--namespace aws-sdk-cpp-test \
--metric-name "New Posts" \
--timestamp "$(date)" \
--value 0.7 \
--unit Count

但是,如果我 运行 cw_put_metric_data 示例,指标将转到 us-east-1 而不是 us-west-2。

编辑: Aws::CloudWatch::CloudWatchClient 使用默认 Aws::Client:ClientConfiguration 实例化,其中区域硬连接到 US_EAST_1。所以问题是如何利用 ~/.aws/credentials?

你可以使用这个class:

https://github.com/aws/aws-sdk-cpp/blob/master/aws-cpp-sdk-core/include/aws/core/config/AWSProfileConfigLoader.h#L59

然后使用那里的值传递给您的客户端配置。