首页 > 解决方案 > 使用 boto3 和 python 列出 s3 存储桶

问题描述

我正在使用下面的代码,并引用了许多SO答案来列出文件夹下的文件boto3python但无法这样做。下面是我的代码:

s3 = boto3.client('s3')
        object_listing = s3.list_objects_v2(Bucket='maxValue',
                                    Prefix='madl-temp/')

我的 s3 路径是"s3://madl-temp/maxValue/“ 我想在其中查找 maxValue 存储桶下是否有任何镶木地板文件,基于这些文件我必须执行以下操作:

If len(maxValue)>0:
 maxValue=true
else:
 maxValue=false

我正在通过Glue作业运行它,但出现以下错误:

botocore.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the ListObjectsV2 operation: The specified bucket does not exist

标签: pythonamazon-web-servicesamazon-s3boto3aws-glue

解决方案


您的存储桶名称是madl-temp,前缀是maxValue。但在 boto3 中,情况正好相反。所以应该是:

s3 = boto3.client('s3')
object_listing = s3.list_objects_v2(Bucket='madl-temp',
                                    Prefix='maxValue/')

要获取您必须执行的文件数:

len(object_listing['Contents']) - 1

where-1占一个前缀maxValue/


推荐阅读