You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量获取AWS账户下所有S3存储桶的请求指标?

获取AWS账户所有S3存储桶请求指标的通用方法

为什么“所有指标”视图查不到?

S3的请求指标属于按存储桶维度上报的自定义指标——默认情况下S3只开启存储类指标,请求指标需要手动为每个存储桶启用。而且CloudWatch的“所有指标”视图不会自动聚合所有存储桶的请求指标,必须通过维度筛选或批量接口查询来获取。

通用解决方案(基于GetMetricsData接口)

1. 先确认所有目标存储桶已启用请求指标

请求指标不会自动开启,得先确保要查询的存储桶都开了:

  • 单个存储桶操作:S3控制台 → 目标存储桶 → 「指标」标签 → 启用请求指标(选“全部请求”就能覆盖AllRequests、GetRequests这类核心指标)
  • 批量启用:用AWS CLI或SDK批量处理,比如用CLI循环调用:
# 先列出所有存储桶
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n' | while read bucket; do
  # 为每个存储桶启用默认的全部请求指标
  aws s3api put-bucket-metrics-configuration --bucket $bucket --id EntireBucket --metrics-configuration '{"Id": "EntireBucket"}'
done

2. 用GetMetricsData批量查询所有存储桶的指标

GetMetricsData支持一次性查询多个指标,你可以通过指定命名空间AWS/S3,结合BucketName和FilterId维度来批量获取所有存储桶的请求指标。

示例:用AWS CLI批量查询

先创建一个metric-queries.json文件(可以用脚本自动生成,不用手动写):

[
  {
    "Id": "bucket1_all_reqs",
    "MetricStat": {
      "Metric": {
        "Namespace": "AWS/S3",
        "MetricName": "AllRequests",
        "Dimensions": [
          {"Name": "BucketName", "Value": "your-bucket-1"},
          {"Name": "FilterId", "Value": "EntireBucket"}
        ]
      },
      "Period": 3600,
      "Stat": "Sum"
    }
  },
  {
    "Id": "bucket1_get_reqs",
    "MetricStat": {
      "Metric": {
        "Namespace": "AWS/S3",
        "MetricName": "GetRequests",
        "Dimensions": [
          {"Name": "BucketName", "Value": "your-bucket-1"},
          {"Name": "FilterId", "Value": "EntireBucket"}
        ]
      },
      "Period": 3600,
      "Stat": "Sum"
    }
  },
  # 其他存储桶的查询以此类推
]

然后执行查询命令:

aws cloudwatch get-metrics-data \
  --metrics-data-queries file://metric-queries.json \
  --start-time 2024-05-01T00:00:00Z \
  --end-time 2024-05-02T00:00:00Z

自动生成查询的脚本示例(Python)

如果存储桶多,手动写查询太麻烦,用Python脚本自动生成查询并调用接口:

import boto3

# 初始化客户端
s3 = boto3.client('s3')
cloudwatch = boto3.client('cloudwatch')

# 获取所有存储桶名称
buckets = [bucket['Name'] for bucket in s3.list_buckets()['Buckets']]

# 构建指标查询列表
metric_queries = []
for idx, bucket in enumerate(buckets):
    # 添加AllRequests查询
    metric_queries.append({
        "Id": f"bucket_{idx}_all",
        "MetricStat": {
            "Metric": {
                "Namespace": "AWS/S3",
                "MetricName": "AllRequests",
                "Dimensions": [
                    {"Name": "BucketName", "Value": bucket},
                    {"Name": "FilterId", "Value": "EntireBucket"}
                ]
            },
            "Period": 3600,
            "Stat": "Sum"
        }
    })
    # 添加GetRequests查询
    metric_queries.append({
        "Id": f"bucket_{idx}_get",
        "MetricStat": {
            "Metric": {
                "Namespace": "AWS/S3",
                "MetricName": "GetRequests",
                "Dimensions": [
                    {"Name": "BucketName", "Value": bucket},
                    {"Name": "FilterId", "Value": "EntireBucket"}
                ]
            },
            "Period": 3600,
            "Stat": "Sum"
        }
    })

# 调用GetMetricsData接口
response = cloudwatch.get_metrics_data(
    MetricsDataQueries=metric_queries,
    StartTime='2024-05-01T00:00:00Z',
    EndTime='2024-05-02T00:00:00Z'
)

# 处理返回结果
for result in response['MetricsDataResults']:
    bucket_name = next(dim['Value'] for dim in result['Label'].split(', ') if dim.startswith('BucketName='))
    metric_name = next(dim['Value'] for dim in result['Label'].split(', ') if dim.startswith('MetricName='))
    print(f"{bucket_name} - {metric_name}: {result['Values']}")

3. 替代方案:用CloudWatch Metric Streams批量导出

如果需要持续获取所有S3请求指标,配置CloudWatch Metric Streams可以把所有AWS/S3命名空间的指标流式传输到S3、Kinesis等服务,适合长期批量处理。

关键注意事项

  • FilterId必须和你启用请求指标时设置的ID一致,默认启用“全部请求”时是EntireBucket
  • 请求指标会产生额外费用,批量启用前请确认AWS计费规则
  • GetMetricsData一次最多支持500个查询,存储桶超过500个时需要分批次调用

内容的提问来源于stack exchange,提问作者Sridip Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:53:10