代码之家  ›  专栏  ›  技术社区  ›  fields

如何判断S3桶中存储了多少对象?

  •  98
  • fields  · 技术社区  · 16 年前

    除非我遗漏了什么,否则我所看到的api似乎都不会告诉您一个系统中有多少个对象 <S3 bucket>/<folder> . 有没有办法数一数?

    21 回复  |  直到 5 年前
        1
  •  44
  •   Giacomo1968 Arvind    11 年前

    没有办法,除非你

    1. 以1000个为一批列出它们(这可能会很慢并且占用带宽—amazon似乎从不压缩XML响应),或者

    2. 在S3上登录您的帐户,然后转到帐户使用。计费部门好像知道你到底存了多少东西!

    如果你存储了5000万个对象,那么下载所有对象的列表实际上需要一些时间和金钱。

    this thread about StorageObjectCount -在使用数据中。

    一个S3API至少可以获得基本的信息,即使它已经有几个小时了,也会很好。

        2
  •  302
  •   Mayank Jaiswal    9 年前

    使用AWS CLI

    aws s3 ls s3://mybucket/ --recursive | wc -l 
    

    aws cloudwatch get-metric-statistics \
      --namespace AWS/S3 --metric-name NumberOfObjects \
      --dimensions Name=BucketName,Value=BUCKETNAME \
                  Name=StorageType,Value=AllStorageTypes \
      --start-time 2016-11-05T00:00 --end-time 2016-11-05T00:10 \
      --period 60 --statistic Average
    

    https://forums.aws.amazon.com/thread.jspa?threadID=217050

    使用AWS Web控制台

    你可以看看 cloudwatch's metric section enter image description here

    我有大约5000万个产品,用了一个多小时才算出来 aws s3 ls

        3
  •  165
  •   advncd    7 年前

    有一个 --summarize 开关,包括 bucket摘要信息 (即对象数量、总大小)。

    以下是使用AWS cli的正确答案:

    aws s3 ls s3://bucketName/path/ --recursive --summarize | grep "Total Objects:"
    
    Total Objects: 194273
    

    看到了吗 documentation

        4
  •  73
  •   gvasquez    9 年前

    虽然这是一个老问题,而且反馈是在2015年提供的,但现在要简单得多,因为S3 Web控制台启用了“获取大小”选项:

    enter image description here

    enter image description here

        5
  •  57
  •   Raphael    10 年前

    现在有一个简单的S3API解决方案(在AWS cli中提供):

    aws s3api list-objects --bucket BUCKETNAME --output json --query "[length(Contents[])]"
    

    或对于特定文件夹:

    aws s3api list-objects --bucket BUCKETNAME --prefix "folder/subfolder/" --output json --query "[length(Contents[])]"
    
        6
  •  51
  •   PriorityMark    11 年前

    如果你使用 s3cmd 命令行工具,可以得到特定bucket的递归列表,将其输出到文本文件中。

    s3cmd ls -r s3://logs.mybucket/subfolder/ > listing.txt
    

    然后在linux中,可以对文件运行wc-l来计算行数(每个对象1行)。

    wc -l listing.txt
    
        7
  •  42
  •   mastaBlasta    10 年前
        8
  •  8
  •   veben    5 年前

    带AWS控制台

    使用AWS Cloudwatch的指标

    使用AWS CLI

    对象数:

    或:

    aws s3api list-objects --bucket <BUCKET_NAME> --prefix "<FOLDER_NAME>" | wc -l
    

    aws s3 ls s3://<BUCKET_NAME>/<FOLDER_NAME>/ --recursive --summarize --human-readable | grep "Total Objects"
    

    s4cmd :

    s4cmd ls -r s3://<BUCKET_NAME>/<FOLDER_NAME>/ | wc -l
    

    aws s3api list-objects --bucket <BUCKET_NAME> --output json --query "[sum(Contents[].Size), length(Contents[])]" | awk  'NR!=2 {print $0;next}  NR==2 {print $0/1024/1024/1024" GB"}'
    

    或:

    aws s3 ls s3://<BUCKET_NAME>/<FOLDER_NAME>/ --recursive --summarize --human-readable | grep "Total Size"
    

    或与 s4cmd公司

    s4cmd du s3://<BUCKET_NAME>
    

    aws cloudwatch get-metric-statistics --metric-name BucketSizeBytes --namespace AWS/S3 --start-time 2020-10-20T16:00:00Z --end-time 2020-10-22T17:00:00Z --period 3600 --statistics Average --unit Bytes --dimensions Name=BucketName,Value=<BUCKET_NAME> Name=StorageType,Value=StandardStorage --output json | grep "Average"
    
        9
  •  6
  •   Kenan    11 年前

    转到AWS帐单,然后是报告,然后是AWS使用情况报告。 选择Amazon Simple Storage Service,然后选择Operation StandardStorage。

        10
  •  4
  •   Tilan Ukwatta    7 年前

    如果您转到s3控制台的“管理”选项卡,然后单击“度量”,您可以很容易地获得总计数和历史记录。。。 Screen shot of the tab

        11
  •  4
  •   Alex Ling Zhong    5 年前

    步骤1:选择根文件夹

    显然,要小心-不要删除它)

    第三步:等几分钟,aws会显示物体的数量和总大小。

        12
  •  3
  •   Jeremy Logan    10 年前

    在s3cmd中,只需运行以下命令(在Ubuntu系统上):

    s3cmd ls -r s3://mybucket | wc -l
    
        13
  •  3
  •   wjordan    9 年前

    在AWS CLI的命令行中,使用 ls plus --summarize

    aws s3 ls "s3://MyBucket" --summarize
    

    这会花费一些时间(列出我的16+K文档大约需要4分钟),但比一次计算1K要快。

        14
  •  3
  •   Eric    7 年前

    Measure-Object 从PowerShell获取文件总数,就像 wc -l

    PS C:\> aws s3 ls s3://mybucket/ --recursive | Measure-Object
    
    Count    : 25
    Average  :
    Sum      :
    Maximum  :
    Minimum  :
    Property :
    

    希望有帮助。

        15
  •  2
  •   BigJoe714    16 年前

    有关更多信息,请参阅此Amazon文档: Iterating Through Multi-Page Results

        16
  •  2
  •   Darby    12 年前

    在特定的桶里 (我不认为账单是按桶来划分的)。

    So, using 3Hub, 
    - list the contents of the bucket (looks basically like a finder or explorer window)
    - go to the bottom of the list, click 'show all'
    - select all (ctrl+a)
    - choose copy URLs from right-click menu
    - paste the list into a text file (I use TextWrangler for Mac) 
    - look at the line count  
    

    我有20521个文件在桶里,做了不到一分钟的文件计数。

        17
  •  2
  •   salsbury    11 年前

    scalablelogic.com (添加计数日志)。效果很好。

    #!/usr/local/bin/python
    
    import sys
    
    from boto.s3.connection import S3Connection
    
    s3bucket = S3Connection().get_bucket(sys.argv[1])
    size = 0
    totalCount = 0
    
    for key in s3bucket.list():
        totalCount += 1
        size += key.size
    
    print 'total size:'
    print "%.3f GB" % (size*1.0/1024/1024/1024)
    print 'total count:'
    print totalCount
    
        18
  •  2
  •   DevOps Dan    5 年前

    截至2020年11月18日,现在有一种更简单的方法来获取此信息,而无需对API请求征税:

    AWS S3 Storage Lens

    enter image description here

    enter image description here

        19
  •  1
  •   Mitch Dempsey    16 年前

    没有一个API会给你一个计数,因为实际上没有任何特定于Amazon的API可以做到这一点。您只需运行一个内容列表并计算返回的结果数。

        20
  •  1
  •   fuzzygroup    6 年前

    下面是上面嵌入的python脚本的boto3版本。

    import sys
    import boto3
    
    s3 = boto3.resource('s3')
    s3bucket = s3.Bucket(sys.argv[1])
    size = 0
    totalCount = 0
    
    for key in s3bucket.objects.all():
        totalCount += 1
        size += key.size
    
    print('total size:')
    print("%.3f GB" % (size*1.0/1024/1024/1024))
    print('total count:')
    print(totalCount)`
    
        21
  •  1
  •   MayurSatpute    6 年前

    aws s3 ls s3://bucket name/文件夹前缀(如果有)--递归的| wc-l

        22
  •  1
  •   Anuj Guleria    5 年前

        23
  •  0
  •   arielcr    12 年前

    3Hub 已停产。有一个更好的解决方案,你可以用 Transmit Show Item Count View 菜单。

        24
  •  0
  •   gubs    10 年前

    您可以从下载并安装s3浏览器 http://s3browser.com/

        25
  •  0
  •   Vivek    8 年前

    您可以潜在地使用amazons3清单,它将为您提供csv文件中的对象列表

        26
  •  0
  •   Ghilas BELHADJ    7 年前

    也可以用 gsutil du (是的,谷歌云工具)

    gsutil du s3://mybucket/ | wc -l
    
        27
  •  0
  •   Indunil Asanka    7 年前

    您只需执行这个cli命令就可以获得bucket或特定文件夹中的文件总数

    扫描整个桶

    aws s3api list-objects-v2 --bucket testbucket | grep "Key" | wc -l
    aws s3api list-objects-v2 --bucket BUCKET_NAME | grep "Key" | wc -l
    

    您可以使用此命令获取详细信息

    aws s3api list-objects-v2 --bucket BUCKET_NAME
    

    aws s3api list-objects-v2 --bucket testbucket --prefix testfolder --start-after testfolder/ | grep "Key" | wc -l
    
    aws s3api list-objects-v2 --bucket BUCKET_NAME --prefix FOLDER_NAME --start-after FOLDER_NAME/ | grep "Key" | wc -l
    
        28
  •  0
  •   tsveti_iko    7 年前

    .jpg 图像,可以执行以下操作:

    aws s3 ls s3://your_bucket | grep jpg | wc -l
    
        29
  •  0
  •   Rakesh    6 年前

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.519</version>
    </dependency>
    
    import com.amazonaws.ClientConfiguration;
    import com.amazonaws.Protocol;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3ClientBuilder;
    import com.amazonaws.services.s3.model.ObjectListing;
    
    public class AmazonS3Service {
    
        private static final String S3_ACCESS_KEY_ID = "ACCESS_KEY";
        private static final String S3_SECRET_KEY = "SECRET_KEY";
        private static final String S3_ENDPOINT = "S3_URL";
    
        private AmazonS3 amazonS3;
    
        public AmazonS3Service() {
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.setProtocol(Protocol.HTTPS);
            clientConfiguration.setSignerOverride("S3SignerType");
            BasicAWSCredentials credentials = new BasicAWSCredentials(S3_ACCESS_KEY_ID, S3_SECRET_KEY);
            AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
            AmazonS3ClientBuilder.EndpointConfiguration endpointConfiguration = new AmazonS3ClientBuilder.EndpointConfiguration(S3_ENDPOINT, null);
            amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider).withClientConfiguration(clientConfiguration)
                    .withPathStyleAccessEnabled(true).withEndpointConfiguration(endpointConfiguration).build();
        }
    
        public int countObjects(String bucketName) {
            int count = 0;
            ObjectListing objectListing = amazonS3.listObjects(bucketName);
            int currentBatchCount = objectListing.getObjectSummaries().size();
            while (currentBatchCount != 0) {
                count += currentBatchCount;
                objectListing = amazonS3.listNextBatchOfObjects(objectListing);
                currentBatchCount = objectListing.getObjectSummaries().size();
            }
            return count;
        }
    }
    
        30
  •  -1
  •   zeroc00l    10 年前

    最简单的方法是使用开发人员控制台,例如,如果您在chrome上,选择developer Tools,您可以看到下面的内容,您可以查找和计数,或者进行一些匹配,比如280-279+1=2

    ...