Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download subset of file from s3 using Boto3

Using boto, I was able to download just a subset of a file from Amazon s3. Given an s3 key, I specified the start and stop bytes and passed them into the get_contents_as_string call.

# Define bytes to focus on
headers={'Range' : 'bytes={}-{}'.format(start_byte, stop_byte)}
resp = key.get_contents_as_string(headers=headers)

Is there a way to accomplish the same task in boto3?

like image 553
user984165 Avatar asked Mar 08 '17 17:03

user984165


People also ask

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.

Can I download multiple files from S3?

Download multiple files from AWS CloudShell using Amazon S3 Now you need to download the contents of the bucket to your local machine. Because the Amazon S3 console doesn't support the downloading of multiple objects, you need to use the AWS CLI tool that's installed on your local machine.


1 Answers

You can use the same Range parameter in get_object() method:

s3 = boto3.client('s3')
resp = s3.get_object(Bucket='bucket', Range='bytes={}-{}'.format(start_byte, stop_byte))
content = resp['Body']
like image 64
franklinsijo Avatar answered Oct 23 '22 14:10

franklinsijo