1

我正在尝试使用 GS Python 库中提供的代码示例从 Google Cloud Storage 下载一个大文件(2.5GB)。这适用于较小的文件(我已经测试了一些 1-2KB 的文件)。我在 Windows 7 上使用 Python 2.7.5。

dest_dir = c:\\downloadfolder
networkbucket = bucketname

uri = boto.storage_uri(networkbucket,'gs')
for obj in uri.get_bucket():
    print obj.name
    name=str(obj.name)
    local_dst_uri = boto.storage_uri(os.path.join(dest_dir, name),'file')
    object_contents = StringIO.StringIO()
    src_uri = boto.storage_uri(networkbucket + '/' + name, 'gs')
    src_uri.get_key().get_file(object_contents)
    object_contents.seek(0)
    local_dst_uri.new_key().set_contents_from_file(object_contents)
    object_contents.close()

我收到内存错误:

Traceback (most recent call last):
File "C:\folder\GS_Transfer.py", line 52, in <module>
src_uri.get_key().get_file(object_contents)
File "C:\gsutil\third_party\boto\boto\gs\key.py", line 165, in get_file
query_args=query_args)
File "C:\gsutil\third_party\boto\boto\s3\key.py", line 1455, in _get_file_internal
for bytes in self:
File "C:\gsutil\third_party\boto\boto\s3\key.py", line 364, in next
data = self.resp.read(self.BufferSize)
File "C:\gsutil\third_party\boto\boto\connection.py", line 414, in read
return httplib.HTTPResponse.read(self, amt)
File "C:\Python27\lib\httplib.py", line 567, in read
s = self.fp.read(amt)
File "C:\Python27\lib\socket.py", line 400, in read
buf.write(data)
MemoryError: out of memory

我可以通过命令行使用 gsutil.py cp 下载文件。不确定如何修改此代码?我一直在尝试找到一种方法来部分下载,但不知道如何。

4

1 回答 1

1

问题是您正在使用StringIO. 你可以使用KeyFile这里的类:

from boto.s3.keyfile import KeyFile

使用它代替StringIO

local_dst_uri = boto.storage_uri(os.path.join(dest_dir, name),'file')
src_uri = boto.storage_uri(networkbucket + '/' + name, 'gs')
keyfile = KeyFile(src_uri.get_key())
local_dst_uri.new_key().set_contents_from_file(keyfile)
于 2013-08-19T16:41:38.707 回答