How do I get the size of a boto3 Collection?

前端 未结 3 1266
攒了一身酷
攒了一身酷 2021-02-07 00:34

The way I have been using is to transform the Collection into a List and query the length:

s3 = boto3.resource(\'s3\')
bucket = s3.Bucket(\'my_bucket\')
size = l         


        
3条回答
  •  失恋的感觉
    2021-02-07 01:10

    For my use case, I just needed to know whether the folder is empty or not.

    s3 = boto3.client('s3')
    response = s3.list_objects(
            Bucket='your-bucket',
            Prefix='path/to/your/folder/',
    )
    print(len(response['Contents']))
    

    This was enough to know whether the folder is empty. Note that a folder, if manually created in the S3 console, can count as a resource itself. In this case, if the length shown above is greater than 1, then the S3 "folder" is empty.

提交回复
热议问题