[英]Download multiple files from S3 bucket using boto3
我有一个包含许多 uuid 的csv文件
我想使用boto3
编写一个 python 脚本:
文件都包含在这样的文件路径中: BUCKET/ORG/FOLDER1/UUID/DATA/FILE.PNG
但是,包含在DATA/
中的文件可以是不同的文件类型。
S3
存储桶中到目前为止,我已经成功连接到 s3 存储桶并使用 boto3 检查了 python 中的内容,但需要帮助实现 rest
import boto3
#Create Session
session = boto3.Session(
aws_access_key_id='ACCESS_KEY_ID',
aws_secret_access_key='SECRET_ACCESS_KEY',
)
#Initiate S3 Resource
s3 = session.resource('s3')
your_bucket = s3.Bucket('BUCKET-NAME')
for s3_file in your_bucket.objects.all():
print(s3_file.key) # prints the contents of bucket
To read the CSV file you can use csv
library (see: https://docs.python.org/fr/3.6/library/csv.html ) Example :
import csv
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
要将文件推送到新存储桶,可以使用copy
方法(参见: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.copy )
示例:
import boto3
s3 = boto3.resource('s3')
source = {
'Bucket': 'BUCKET-NAME',
'Key': 'mykey'
}
bucket = s3.Bucket('SECOND_BUCKET-NAME')
bucket.copy(source, 'SECOND_BUCKET-NAME')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.