[英]How to list the files in S3 subdirectory using Python
我试图在S3的子目录下列出文件,但无法列出文件名:
import boto
from boto.s3.connection import S3Connection
access=''
secret=''
conn=S3Connection(access,secret)
bucket1=conn.get_bucket('bucket-name')
prefix='sub -directory -path'
print bucket1.list(prefix)
files_list=bucket1.list(prefix,delimiter='/')
print files_list
for files in files_list:
print files.name
您能帮我解决这个问题吗?
您可以通过在前缀末尾添加/
来修复您的代码。
import boto3
s3 = boto3.resource('s3')
## Bucket to use
bucket = s3.Bucket('my-bucket')
## List objects within a given prefix
for obj in bucket.objects.filter(Delimiter='/', Prefix='fruit/'):
print obj.key
输出:
fruit/apple.txt
fruit/banana.txt
该代码不使用S3客户端,而是使用boto3提供的S3对象,这使某些代码更简单。
您可以使用boto3做到这一点。 列出所有文件。
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
objs = list(bucket.objects.filter(Prefix='sub -directory -path'))
for i in range(0, len(objs)):
print(objs[i].key)
这段代码将打印子目录中存在路径的所有文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.