[英]Saving an object to CSV directly to S3
I have a file-like object received through HTTP response, I want to directly save it as CSV in S3.我有一个通过 HTTP 响应收到的类似 object 的文件,我想在 S3 中直接将其另存为 CSV。 I tried using the S3 bucket and its path but I am facing an error, as no such file was found
我尝试使用 S3 存储桶及其路径,但遇到错误,因为找不到此类文件
Can someone help me with the code here- r5 is the URL response, rr= r5.raw which has the response data object.有人可以帮我这里的代码 - r5 是 URL 响应,rr= r5.raw 具有响应数据 object。 This object has to be saved as CSV into S3 directly.
这个 object 必须作为 CSV 直接保存到 S3 中。
Docs with example of sending existing file: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html带有发送现有文件示例的文档: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html
If you have a file content in a string, use BytesIO
to make it a file-like object:如果您在字符串中有文件内容,请使用
BytesIO
使其成为类似文件的 object:
s3 = boto3.resource("s3")
bucket = s3.Bucket(bucket_name)
stream = io.BytesIO(my_str_data.encode()) # boto3 only allows to bytes data, so we need to encode
filename = "filename_here"
bucket.upload_fileobj(stream, filename)
Since you said you already have a file-like object, make sure it's bytes file-like object and just put in in place of stream
既然你说你已经有一个类似 object 的文件,请确保它是类似 object 的字节文件,然后替换
stream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.