[英]Downloading from google cloud storage with api client library fails with HTTP 416
unfortunately I don't want to rely on additional packages except of the googleapiclient
one and facing some issue on downloading objects from a storage bucket. 不幸的是,我不想依赖除了
googleapiclient
之外的其他软件包,并且面临从存储桶下载对象的一些问题。
from googleapiclient.http import MediaIoBaseDownload
import googleapiclient.discovery
storage_service = googleapiclient.discovery.build(
serviceName='storage', version='v1', credentials=creds)
f = storage_service.objects()
results = f.list(bucket='MYBUCKET').execute()
for d in results['items']:
with open(d['name']), 'wb') as fh:
req = MediaIoBaseDownload(
fh,
f.get_media(bucket=d['bucket'], object=d['name'], generation=d['generation']),
chunksize=1024*1024
)
done = False
while done is False:
status, done = req.next_chunk()
Now this yieldds the following error: 现在这会产生以下错误:
---------------------------------------------------------------------------
HttpError Traceback (most recent call last)
<ipython-input-210-d66ce751dec5> in <module>()
4 done = False
5 while done is False:
----> 6 status, done = req.next_chunk()
path_to_my_env/lib/python2.7/site-packages/googleapiclient/_helpers.pyc in positional_wrapper(*args, **kwargs)
128 elif positional_parameters_enforcement == POSITIONAL_WARNING:
129 logger.warning(message)
--> 130 return wrapped(*args, **kwargs)
131 return positional_wrapper
132
path_to_my_env/lib/python2.7/site-packages/googleapiclient/http.pyc in next_chunk(self, num_retries)
703 return MediaDownloadProgress(self._progress, self._total_size), self._done
704 else:
--> 705 raise HttpError(resp, content, uri=self._uri)
706
707
HttpError: <HttpError 416 when requesting https://www.googleapis.com/storage/v1/b/MYBUCKET/o/MYOBJECT?generation=1234&alt=media returned "Requested range not satisfiable">
Is somebody aware of what I'm missing somewhere or what is best practice to download files from storage? 是否有人知道我在某处丢失了什么或从存储中下载文件的最佳做法是什么? Everything I found relies on storage specific libraries.
我发现的一切都依赖于存储特定的库。
This seems to be related to this issue . 这似乎与这个问题有关 。
Some of the files (most) are 0-byte files. 一些文件(大多数)是0字节文件。
Problem is fixed by the following code: 问题由以下代码修复:
for d in results['items']:
request = f.get_media(bucket=d['bucket'], object=d['name'], generation=d['generation'])
response = request.execute()
if response != '':
with open(d['name']), 'wb') as fh:
fh.write(response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.