user984165
user984165

Reputation: 302

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?

Upvotes: 10

Views: 6409

Answers (1)

franklinsijo
franklinsijo

Reputation: 18270

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']

Upvotes: 14

Related Questions