Home > Blockchain >  Saving an object to CSV directly to S3
Saving an object to CSV directly to S3

Time:09-14

I have a file-like object received through HTTP response, I want to directly save it as CSV in S3. I tried using the S3 bucket and its path but I am facing an error, as no such file was found

Can someone help me with the code here- r5 is the URL response, rr= r5.raw which has the response data object. This object has to be saved as CSV into S3 directly.

CodePudding user response:

Docs with example of sending existing file: 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:

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

  • Related