[英]How to retrieve file names from S3 bucket and all of the subfolders
我正在嘗試從我的 s3 存儲桶中檢索所有文件名。 但我不需要所有東西,只需要存儲桶中某些位置的文件名。 這樣做有困難。 我正在嘗試使用 Boto3.Resource 但運氣不佳。 我能夠連接到存儲桶並檢索存儲桶的所有名稱。 但是當涉及到子文件夾時,就會出現問題。
這是我的代碼:
import boto3
s3 = boto3.resource(
service_name = 's3',
region_name = 'us-west-1',
aws_access_key_id = 'XXXXXXXXXXXXX',
aws_secret_access_key = 'YYYYYYYYYYYYYYY')
for x in s3.buckets.all():
print(x)
# I am able to list the names of the buckets.
# loop through all buckets
for x in s3.buckets.all():
# make sure this is my bucket.
if x == 'my-data-bucket':
objs = list(x.objects.filter(Delimiter = '', Prefix='my-data-bucket/helpdesk/production/docs/'))
for i in range(0, len(objs)):
print(objs[i].key)
# print all the file names.
請記住,“docs”子文件夾還有其他子文件夾,如 001、002、003 等,每個文件夾也有文檔
目標是在類似於此格式的 csv 文件中列出所有文件名
/my-data-bucket
/helpdesk/production/docs/001/x.pdf
/helpdesk/production/docs/001/zz.pdf
/helpdesk/production/docs/002/aa.pdf
/helpdesk/production/docs/003/rr.pdf
etc .....
我如何解決這個問題,我能夠將所有對象放在一個列表中,如下所示:
all_objects = list(s3.Bucket('Bucket_Name').objects.all())
looped through all objects:
for x in all_objects:
# appended all objects to different list
create a dictionary of lists like dict = {'x': list1, 'y': list2, ....}
created a dataframe (df = pd.DataFrame(dict))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.