使用boto3列出存储桶内的内容
使用boto3列出存储桶内的内容
我如何使用boto3查看S3存储桶中的内容(例如执行\"ls\"
命令)?
尝试以下操作:
import boto3 s3 = boto3.resource('s3') my_bucket = s3.Bucket('some/path/')
会返回:
s3.Bucket(name='some/path/')
我该如何查看其内容?
admin 更改状态以发布 2023年5月23日
这类似于“ls”,但它不考虑前缀文件夹约定,并且将列出 Bucket 中的对象。过滤掉作为 Key 名称一部分的前缀取决于读者。
在 Python 2 中:
from boto.s3.connection import S3Connection conn = S3Connection() # assumes boto.cfg setup bucket = conn.get_bucket('bucket_name') for obj in bucket.get_all_keys(): print(obj.key)
在 Python 3 中:
from boto3 import client conn = client('s3') # again assumes boto.cfg setup, assume AWS S3 for key in conn.list_objects(Bucket='bucket_name')['Contents']: print(key['Key'])