Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve S3 file as Object instead of downloading to absolute system path

I just started learning and using S3, read the docs. Actually I didn't find anything to fetch the file into an object instead of downloading it from S3? if this could be possible, or I am missing something?

Actually I want to avoid additional IO after downloading the file.

like image 715
Bruce_Wayne Avatar asked May 07 '16 10:05

Bruce_Wayne


People also ask

Can I read S3 file without downloading?

Reading objects without downloading them Similarly, if you want to upload and read small pieces of textual data such as quotes, tweets, or news articles, you can do that using the S3 resource method put(), as demonstrated in the example below (Gist).

How do I extract files from S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

How do I get to S3 path?

Get an S3 Object's URL #Navigate to the AWS S3 console and click on your bucket's name. Use the search input to find the object if necessary. Click on the checkbox next to the object's name. Click on the Copy URL button.


2 Answers

You might be looking for the get_object() method of the boto3 S3 client:

http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.get_object

This will get you a response object dictionary with member Body that is a StreamingBody object, which you can use as normal file and call .read() method on it. To get the entire content of the S3 object into memory you would do something like this:

s3_client = boto3.client('s3') s3_response_object = s3_client.get_object(Bucket=BUCKET_NAME_STRING, Key=FILE_NAME_STRING) object_content = s3_response_object['Body'].read() 
like image 118
grepe Avatar answered Nov 05 '22 14:11

grepe


I prefer this approach, equivalent to a previous answer:

import boto3 s3 = boto3.resource('s3') def read_s3_contents(bucket_name, key):     response = s3.Object(bucket_name, key).get()     return response['Body'].read() 

But another approach could read the object into StringIO:

import StringIO import boto3 s3 = boto3.resource('s3') def read_s3_contents_with_download(bucket_name, key):     string_io = StringIO.StringIO()     s3.Object(bucket_name, key).download_fileobj(string_io)     return string_io.getvalue() 
like image 21
Carl G Avatar answered Nov 05 '22 14:11

Carl G