我有一個包含大量uuid的 csv 檔案
我想撰寫一個 python 腳本,使用boto3它:
- 連接到 AWS S3 存盤桶
- 使用 CSV 中包含的每個 uuid 來復制包含的檔案
檔案都包含在這樣 BUCKET/ORG/FOLDER1/UUID/DATA/FILE.PNG
的檔案路徑中:但是,其中包含的檔案DATA/可以是不同的檔案型別。
- 將復制的檔案放入新的
S3存盤桶中
到目前為止,我已成功連接到 s3 存盤桶并使用 boto3 在 python 中檢查其內容,但需要幫助實作其余部分
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
uj5u.com熱心網友回復:
要讀取 CSV 檔案,您可以使用csv庫(請參閱:https : //docs.python.org/fr/3.6/library/csv.html)
示例:
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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372071.html
標籤:Python 亚马逊网络服务 文件 亚马逊-s3 boto3
上一篇:有誰知道這在實體中的去向?
