Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading part of a file in S3 using Boto

I am trying to read 700MB file stored in S3. How ever I only require bytes from locations 73 to 1024.

I tried to find a usable solution but failed to. Would be a great help if someone could help me out.

like image 780
BMC Avatar asked May 06 '15 11:05

BMC


2 Answers

S3 supports GET requests using the 'Range' HTTP header which is what you're after.

To specify a Range request in boto, just add a header dictionary specifying the 'Range' key for the bytes you are interested in. Adapted from Mitchell Garnaat's response:

import boto
s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')
your_bytes = key.get_contents_as_string(headers={'Range' : 'bytes=73-1024'})
like image 130
Josh Kupershmidt Avatar answered Sep 24 '22 23:09

Josh Kupershmidt


import boto3

obj = boto3.resource('s3').Object('mybucket', 'mykey')
stream = obj.get(Range='bytes=32-64')['Body']
print(stream.read())

boto3 version from https://github.com/boto/boto3/issues/1236

like image 33
Dyno Fu Avatar answered Sep 23 '22 23:09

Dyno Fu