Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the contents of an S3 file without downloading it using boto3?

I am working on a process to dump files from a Redshift database, and would prefer not to have to locally download the files to process the data. I saw that Java has a StreamingObject class that does what I want, but I haven't seen anything similar in boto3.

like image 219
flybonzai Avatar asked Aug 11 '16 20:08

flybonzai


2 Answers

If you have a mybucket S3 bucket, which contains a beer key, here is how to download and fetch the value without storing it in a local file:

import boto3
s3 = boto3.resource('s3')
print s3.Object('mybucket', 'beer').get()['Body'].read()
like image 174
johntellsall Avatar answered Oct 09 '22 06:10

johntellsall


This may or may not be relevant to what you want to do, but for my situation one thing that worked well was using tempfile:

import tempfile
import boto3
    
bucket_name = '[BUCKET_NAME]'
key_name = '[OBJECT_KEY_NAME]'
s3 = boto3.resource('s3')
temp = tempfile.NamedTemporaryFile()
s3.Bucket(bucket_name).download_file(key_name, temp.name)
# do what you will with your file...
temp.close()
like image 1
James Shapiro Avatar answered Oct 09 '22 08:10

James Shapiro