简体   繁体   English

使用HTTP 416从带有api客户端库的谷歌云存储下载失败

[英]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.

相关问题 从 Google Cloud Storage 下载文件 - Downloading a file from Google Cloud Storage 使用于访问谷歌云存储的谷歌python客户端库达到了一个存根API - Make the Google python client library for accessing Google cloud storage hit a stubbed API 使用Python api-client-library将文本文件上传到Google Cloud Storage。 适用于图片,而非文字 - Upload text file to Google Cloud Storage with Python api-client-library. Works with image, not text 从字节上传谷歌云存储失败 - google cloud storage upload from bytes fails 本地客户端上带有远程 api 的 Google 云存储客户端 - Google cloud storage client with remote api on local client 从文件夹中的谷歌云存储下载文件 - Downloading a file from google cloud storage inside a folder 将文件从 Google Cloud Storage 下载到远程服务器 - Downloading Files from Google Cloud Storage to Remote Server 通过pip安装Google Cloud Storage客户端库-Python GAE - Install Google Cloud Storage Client Library with pip — Python GAE 在开发服务器上使用Google Cloud Storage客户端库显示图像 - Displaying an image with Google Cloud Storage client library on dev server 自1.8.8版开始,devserver上的Google Cloud Storage Client Library 400错误 - Google Cloud Storage Client Library 400 error on devserver as of update 1.8.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM