【问题标题】:Reading part of a file in S3 using Boto使用 Boto 在 S3 中读取文件的一部分
【发布时间】:2015-07-16 13:04:56
【问题描述】:

我正在尝试读取存储在 S3 中的 700MB 文件。但是我只需要从位置 73 到 1024 的字节。

我试图找到一个可用的解决方案,但没有成功。如果有人可以帮助我,那将是一个很大的帮助。

【问题讨论】:

标签: python python-2.7 amazon-s3 cloud boto


【解决方案1】:

S3 支持GET requests using the 'Range' HTTP header,这正是您所追求的。

要在 boto 中指定 Range 请求,只需添加一个标头字典,为您感兴趣的字节指定“Range”键。改编自 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'})

【讨论】:

  • 这可能是获取内容并保留带宽和内存使用的最佳方式。需要小心获取结果,并按照建议使用 get_contents_as_string。 +1
【解决方案2】:
import boto3

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

来自https://github.com/boto/boto3/issues/1236的boto3版本

【讨论】:

    【解决方案3】:

    请看这里的python脚本

    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()
    

    【讨论】:

      猜你喜欢
      • 2015-04-21
      • 1970-01-01
      • 2017-09-07
      • 2017-04-18
      • 1970-01-01
      • 2011-04-26
      • 2014-12-12
      • 2018-01-09
      • 2016-09-23
      相关资源
      最近更新 更多