简体   繁体   English

使用 boto3 从 S3 存储桶下载多个文件

[英]Download multiple files from S3 bucket using boto3

I have a csv file containing numerous uuids我有一个包含许多 uuid 的csv文件

I'd like to write a python script using boto3 which:我想使用boto3编写一个 python 脚本:

  1. Connects to an AWS S3 bucket连接到 AWS S3 存储桶
  2. Uses each uuid contained in the CSV to copy the file contained使用 CSV 中包含的每个 uuid 来复制包含的文件

Files are all contained in a filepath like this: BUCKET/ORG/FOLDER1/UUID/DATA/FILE.PNG However, the file contained in DATA/ can be different file types.文件都包含在这样的文件路径中: BUCKET/ORG/FOLDER1/UUID/DATA/FILE.PNG但是,包含在DATA/中的文件可以是不同的文件类型。

  1. Put the copied file in a new S3 bucket将复制的文件放入新的S3存储桶中

So far, I have successfully connected to the s3 bucket and checked its contents in python using boto3, but need help implementing the rest到目前为止,我已经成功连接到 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 : 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)

To push files to the new bucket, you can use the copy method (see: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.copy )要将文件推送到新存储桶,可以使用copy方法(参见: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.copy

Example :示例

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM