Aws使用java api获得S3 Bucket Size

时间:2016-02-13 09:26:34

标签: java amazon-s3

我在google上搜索了有关获取有关S3存储桶的元数据的有效方法,例如其大小和文件数量。我发现this链接讨论了这个问题。但它适用于PHP和使用云监视的aws cli。我想知道是否有一些java api来获取s3存储桶元数据?

由于

1 个答案:

答案 0 :(得分:1)

您可以在此处找到AWS S3 Java库的大量文档:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/overview-summary.html

回答你的问题,你可以使用getSize()获取S3中对象的大小,你可以遍历所有文件以获得你的存储桶大小。

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/S3ObjectSummary.html#getSize()

S3不支持直接收集磁盘使用情况(意味着不会遍历所有项目),但您可以使用CloudWatch通过单个请求获取您想要使用的数据。

示例查询:

aws cloudwatch get-metric-statistics --namespace AWS/S3 --start-time 2016-01-01T10:00:00 --end-time 2016-02-12T01:00:00 --period 86400 --statistics Average --region us-east-1 --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=www.streambrightdata.com Name=StorageType,Value=StandardStorage

返回:

{
    "Datapoints": [
        {
            "Timestamp": "2016-02-05T10:00:00Z",
            "Average": 54027423.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-03T10:00:00Z",
            "Average": 52917504.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-04T10:00:00Z",
            "Average": 53417421.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-07T10:00:00Z",
            "Average": 54949563.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-01T10:00:00Z",
            "Average": 24951965.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-02T10:00:00Z",
            "Average": 28254636.0,
            "Unit": "Bytes"
        },
        {
            "Timestamp": "2016-02-06T10:00:00Z",
            "Average": 54577328.0,
            "Unit": "Bytes"
        }
    ],
    "Label": "BucketSizeBytes"
}

适用于CloudWatch的AWS Java SDK:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudwatch/AmazonCloudWatchClient.html