繁体   English   中英

如何访问 s3 中创建的文件夹以将 csv 文件写入其中?

[英]How can I access the created folder in s3 to write csv file into it?

我已经创建了文件夹代码,但如何访问该文件夹以将 csv 文件写入该文件夹?

# Creating folder on S3 for unmatched data

client = boto3.client('s3')

# Variables

target_bucket = obj['source_and_destination_details']['s3_bucket_name']

subfolder = obj['source_and_destination_details']['s3_bucket_uri-new_folder_path'] + obj['source_and_destination_details']['folder_name_for_unmatched_column_data']

# Create subfolder (objects)

client.put_object(Bucket = target_bucket, Key = subfolder)

通过上面的代码成功创建了文件夹,但是如何将 csv 文件写入其中呢?

以下是我尝试编写但无法正常工作的代码

# Writing csv on AWS S3

df.reindex(idx).to_csv(obj['source_and_destination_details']['s3_bucket_uri-write'] + obj['source_and_destination_details']['folder_name_for_unmatched_column_data'] + obj['source_and_destination_details']['file_name_for_unmatched_column_data'], index=False)

S3 存储桶不是文件系统。

我假设to_csv()方法应该写入某种文件系统,但这不是它与 S3 一起工作的方式。 虽然有将 S3 存储桶挂载为文件系统的解决方案,但这不是首选方式。

通常,您会通过AWS REST APIAWS CLI或客户端库(例如您已经在使用的 Boto)与 S3 交互。

因此,为了将您的内容存储在 S3 上,您首先要在本地创建文件,例如在系统的/tmp文件夹中。 然后使用 Boto 的put_object()方法上传文件。 之后从本地存储中删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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