【问题标题】:How could I list all objects under a prefix in S3如何在 S3 中列出前缀下的所有对象
【发布时间】:2018-02-15 02:43:28
【问题描述】:

前缀下有超过 3k 个对象。我使用以下代码列出所有对象以获取它们的名称,但 API 仅检索 1000 个对象。 s3_client = boto3.client('s3')

response = s3_client.list_objects(
    Bucket = "my-bucket",
    Prefix = "my-prefix",
    MaxKeys=50000
)


s3 = boto3.resource('s3')
bucket = s3.Bucket(S3)

print(len(response['Contents'])) # only retrieve 1000

【问题讨论】:

    标签: amazon-web-services amazon-s3 boto3


    【解决方案1】:

    使用分页器循环浏览多个页面。见:Creating Paginators

    import boto3
    
    client = boto3.client('s3')
    paginator = client.get_paginator('list_objects')
    operation_parameters = {'Bucket': 'my-bucket',
                            'Prefix': 'my-prefix'}
    page_iterator = paginator.paginate(**operation_parameters)
    for page in page_iterator:
        print(page['Contents'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2020-02-11
      • 2021-05-06
      相关资源
      最近更新 更多