[英]python: FileNotFoundError: [Errno 2] No such file or directory
[英]AWS Glue Python FileNotFoundError: [Errno 2] No such file or director
我正在嘗試使用 AWS Glue 在跨賬戶 S3 存儲桶之間移動文件。 我正在將 Glue 與 python shell 一起使用。 我有源存儲桶的列表和獲取對象權限。 我能夠列出所有文件,但是當我嘗試將文件加載到目標存儲桶時,出現錯誤“FileNotFoundError: [Errno 2] No such file or directory: 'test/f1=x/type=b/file1.鑲木地板'
源 s3 上的文件有分區:
test/f1=x/type=a/file1.parquet
test/f1=x/type=a/file2.parquet
test/f1=x/type=b/file1.parquet
test/f1=x/type=b/file2.parquet
我只是想用 f1=x 和 type=b 加載文件
import pandas as pd
import boto3
client = boto3.client('s3')
bucket = 'mysourcebucketname'
folder_path = 'test/f1=x/type=b/'
def my_keys(bucket,folder_path):
keys = []
resp = client.list_objects(Bucket=bucket, Prefix=folder_path)
for obj in resp['Contents']:
keys.append(obj['Key'])
return keys
files = my_keys(bucket,folder_path)
#print(files)
for file in files:
bucketdest = 'mydestinationbucket'
new_file_name = file.split('/')[-1]
s3_file = 'destfolder1/destfolder2/'+"typeb"+new_file_name
client.upload_file(file,bucketdest,s3_file,ExtraArgs={'GrantFullControl':'id =""})
upload_file用於從本地驅動器上傳到 S3。 因此,您的代碼正在尋找一個名為test/f1=x/type=b/file1.parquet
的本地文件,該文件顯然不存在,因為它在您編寫時位於 S3 上。 也許您想下載這些文件?
這可以使用:
def move_files(BUCKET, SOURCE, DESTINATION, FILENAME):
session = Session(aws_access_key_id= <Access_ID>,
aws_secret_access_key= <Secret Key>)
s3_resource = session.resource('s3')
destination_key = DESTINATION + FILENAME
source_key = SOURCE + FILENAME
try:
s3_resource.Object(BUCKET, destination_key).copy_from(
CopySource=BUCKET + '/' + source_key)
s3_resource.Object(BUCKET, source_key).delete()
except Exception as error:
print(error)
還要確保您的 IAM 用戶可以訪問這兩個存儲桶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.