Reading part of a file in S3 using Boto

守給你的承諾、 提交于 2019-12-20 02:48:19

问题


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.


回答1:


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'})



回答2:


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




回答3:


Please have a look on the python script here

import boto3
region = 'us-east-1' # define your region here
bucketname = 'test'  # define bucket
key = 'objkey' # s3 file 
Bytes_range = 'bytes=73-1024'
client = boto3.client('s3',region_name = region)
resp = client.get_object(Bucket=bucketname,Key=key,Range=Bytes_range)
data = resp['Body'].read()


来源:https://stackoverflow.com/questions/30075978/reading-part-of-a-file-in-s3-using-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!