简体   繁体   English

如何使用Pandas将文件写入S3

[英]How to write a file to S3 using Pandas

I want to write a data frame column in .ann format to S3. 我想以.ann格式将数据帧列写入S3。

Right now I am using the following code to do that. 现在我使用以下代码来做到这一点。

df['user_input'].to_csv(ann_file_path, header=None, index=None, sep=' ')

Where ann_file_path is the full path of the .ann file on the Server. 其中ann_file_path是服务器上.ann文件的完整路径。

I am getting following error message: 我收到以下错误消息:

[Errno 22] Invalid argument: 'https://s3-eu-west-1.amazonaws.com/bucket/sub_folder/somefile.ann'

Why am I getting that? 我为什么要这样做?

Also, do I need to use Boto3 to write or can I directly write the file on S3 with full path? 另外,我是否需要使用Boto3进行编写,还是可以使用完整路径直接在S3上写入文件?

I can think of some authorization might be required for that but the error message seems different from something related to authorization. 我可以想到可能需要一些授权,但错误消息似乎与授权相关的内容不同。

I've resolved. 我已经解决了。 We need AWS handshake using access_key_id and secret_key for AWS. 我们需要使用AWS的access_key_idsecret_key进行AWS握手。

Get URL starting from the bucket name (not https:/...), hence get rid of whatever before it. 从存储桶名称(而不是https:/ ...)开始获取URL,从而摆脱之前的任何内容。

My URL: https://s3-eu-west-1.amazonaws.com/bucket/sub_folder/somefile.ann 我的网址: https://s3-eu-west-1.amazonaws.com/bucket/sub_folder/somefile.annhttps://s3-eu-west-1.amazonaws.com/bucket/sub_folder/somefile.ann

Transformed to: bucket/sub_folder/somefile.ann 转换为: bucket/sub_folder/somefile.ann

Code to do that: ann_file_path = ann_file_path.split('.com/', 1)[1] 执行此操作的代码: ann_file_path = ann_file_path.split('.com/', 1)[1]

Once I got ann_file_path , I used s3fs python library to upload the ann file to the server. 一旦我得到了ann_file_path ,我用s3fs Python库上传安文件到服务器。

bytes_to_write = df['user_input'].to_csv(header=None, index=None).encode()
fs = s3fs.S3FileSystem(key=settings.AWS_ACCESS_KEY_ID, secret=settings.AWS_SECRET_ACCESS_KEY)
with fs.open(ann_file_path, 'wb') as f:
   f.write(bytes_to_write)

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

相关问题 如何在python的S3中从pandas数据帧写入镶木地板文件 - How to write parquet file from pandas dataframe in S3 in python 无法将带有csv扩展的文件从pandas写入AWS S3 - Not able to write file with csv extention into AWS S3 from pandas json文件如何写入s3 parquet - How to write the json file in s3 parquet 如何将数据帧更新/写入S3存储桶中的Excel文件 - How to update/write a dataframe into an excel file located in S3 bucket 如何使用 pandas 读取文本文件的键、值对并写入 csv? - How to read text file's key, value pair using pandas and write to csv? 使用熊猫将文件上传到 s3 时出现印度卢比符号 UnicodeEncodeError - indian rupee symbol UnicodeEncodeError while uploading file to s3 using pandas 无法使用 pandas.read_sas 从 s3 原始存储桶读取.xpt 文件 - Unable to read .xpt file from s3 raw bucket using pandas.read_sas 使用 boto3 检索 s3 存储桶中多个子文件夹中的最后一个文件并写入 sqlite3 db - Retrieve last file within multiple subfolders in an s3 bucket using boto3 and write to sqlite3 db 如何使用 Lambda 函数将下载的文件上传到 s3 存储桶 - How to upload downloaded file to s3 bucket using Lambda function 如何使用 python boto3 重命名 s3 文件名 - How to rename the s3 file name by using python boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM