【问题标题】:AWS Glue Python FileNotFoundError: [Errno 2] No such file or directorAWS Glue Python FileNotFoundError:[Errno 2] 没有这样的文件或目录
【发布时间】:2021-09-14 17:32:59
【问题描述】:

我正在尝试使用 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 =""})

【问题讨论】:

    标签: python amazon-web-services amazon-s3 boto3 aws-glue


    【解决方案1】:

    upload_file 用于从本地驱动器上传到 S3。因此,您的代码正在寻找一个名为 test/f1=x/type=b/file1.parquet 的本地文件,该文件显然不存在,因为它在您编写的 S3 上。也许您想下载这些文件?

    【讨论】:

    • 感谢您的回复。我不想下载文件。我可以不下载就上传吗?
    • @anaz8 从 S3 上传到哪里?
    • 是的将文件从源 s3 移动到目标 s3 存储桶而不下载
    • @anaz8 是的,你可以使用copy_object 然后delete_object 原来的那个。
    • @anaz8 进展如何?还不清楚自己能做什么?
    【解决方案2】:

    这可以使用:

    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 用户对两个存储桶都具有访问权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多