Bru*_*orn 9 amazon-s3 amazon-web-services python-3.x amazon-cloudwatch
我正在编写一个 Python 3 脚本,旨在使用 Boto3 库从 AWS CloudFront 获取 S3 空间利用率统计信息。
我从 AWS CLI 开始,发现我可以通过这样的命令获得我想要的东西:
aws cloudwatch get-metric-statistics --metric-name BucketSizeBytes --namespace AWS/S3 --start-time 2017-03-06T00:00:00Z --end-time 2017-03-07T00:00:00Z --statistics Average --unit Bytes --region us-west-2 --dimensions Name=BucketName,Value=foo-bar Name=StorageType,Value=StandardStorage --period 86400 --output json
Run Code Online (Sandbox Code Playgroud)
这将返回我期望的数据。现在我想在 Python 3 / Boto3 中做同样的事情。到目前为止我的代码是:
aws cloudwatch get-metric-statistics --metric-name BucketSizeBytes --namespace AWS/S3 --start-time 2017-03-06T00:00:00Z --end-time 2017-03-07T00:00:00Z --statistics Average --unit Bytes --region us-west-2 --dimensions Name=BucketName,Value=foo-bar Name=StorageType,Value=StandardStorage --period 86400 --output json
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到一个有效的响应,但没有数据点(它是一个空数组)。它们似乎是相同的,除了 Python 方法似乎没有该区域的位置,而命令行需要它。
我尝试的另一件事是:我的代码正在计算最后一个日期的日期与硬编码的命令行。我确实尝试对日期进行硬编码,只是为了看看是否能取回数据,结果是一样的。
所以我的问题是:
我在 Boto/Python 中使用的方法是否等同于命令行?假设他们是,我会错过什么?
我没有发现您的代码有任何明显的错误,因此该区域看起来像是这里的主要嫌疑人。
您可以在创建客户端时设置它:
cloudwatch = boto3.client('cloudwatch', region_name='us-west-2')
Run Code Online (Sandbox Code Playgroud)
如果未设置,boto 将尝试AWS_DEFAULT_REGION首先从环境变量获取区域,然后再从~/.aws/config配置文件获取区域。尝试检查这些以查看默认区域设置是什么。