如何使用 boto3 遍历 S3 存储桶?

Big*_*337 0 python amazon-s3 boto amazon-web-services boto3

我正在尝试训练一个神经网络,我在其中传递一系列图像。我想创建一个生成器,将每个图像作为 numpy 数组传入

from skimage import io
image_array = io.imread(url)
Run Code Online (Sandbox Code Playgroud)

但这仅适用于特定的亚马逊 aws 网址。我知道使用 boto 库的标准方法是这样的:

s3 = boto3.resource('s3')
s3.meta.client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt') 
Run Code Online (Sandbox Code Playgroud)

但是再次在这里,您似乎指向特定资源

我想要这样的东西:

def my_generator():
    for object in s3_bucket():     # does an s3_bucket() iterator like this exist?
        image_array = io.imread(object)
        yield image_array
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

kar*_*daj 7

您可以执行以下操作:

for key in bucket.objects.all():
    print(key.key)
Run Code Online (Sandbox Code Playgroud)