使用Python从Google云存储下载大文件

使用Python从Google云存储下载大文件,python,google-cloud-storage,gsutil,Python,Google Cloud Storage,Gsutil,我试图使用GS Python库中提供的代码示例从Google云存储下载一个大文件(2.5GB)。这适用于较小的文件(我已经测试了一些1-2KB的文件)。我正在Windows7上使用Python 2.7.5 dest_dir = c:\\downloadfolder networkbucket = bucketname uri = boto.storage_uri(networkbucket,'gs') for obj in uri.get_bucket(): print obj.nam

我试图使用GS Python库中提供的代码示例从Google云存储下载一个大文件(2.5GB)。这适用于较小的文件(我已经测试了一些1-2KB的文件)。我正在Windows7上使用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
回溯(最近一次呼叫最后一次):
文件“C:\folder\GS\u Transfer.py”,第52行,在
src_uri.get_key().get_文件(对象内容)
文件“C:\gsutil\third\u party\boto\boto\gs\key.py”,第165行,在get\u文件中
查询参数=查询参数)
文件“C:\gsutil\third\u party\boto\boto\s3\key.py”,第1455行,在get\u文件\u internal中
对于self中的字节:
文件“C:\gsutil\third\u party\boto\boto\s3\key.py”,第364行,下一行
数据=self.resp.read(self.BufferSize)
文件“C:\gsutil\third\u party\boto\boto\connection.py”,第414行,已读
返回httplib.HTTPResponse.read(self,amt)
文件“C:\Python27\lib\httplib.py”,第567行,已读
s=自身fp.read(金额)
文件“C:\Python27\lib\socket.py”,第400行,已读
buf.写入(数据)
内存错误:内存不足

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

问题是您正在使用
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)

问题是您正在使用
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)

内存不足:正在向内存中的对象读取2.5GB的数据<代码>StringIO不支持磁盘存储。你的内存用完了。为什么不在这里使用文件?内存不足:正在向内存中的对象读取2.5GB的数据<代码>StringIO不支持磁盘存储。你的内存用完了。你为什么不在这里使用文件?