【问题标题】:google storage python JSON client eternal 502 when uploading media to bucket将媒体上传到存储桶时,谷歌存储python JSON客户端永恒502
【发布时间】:2016-02-11 03:45:43
【问题描述】:

这是我第一次使用 Google Storage json api,我正在尝试使用 python 客户端库将一个简单的图像文件 UPLOAD 放到存储桶中。似乎我正确地获取了凭据(应用程序默认),因为当我向 discovery.build() 方法提供凭据时,我可以 LIST 存储桶的内容,然后相同的列表调用在以下情况下失败我不提供凭据。就 ACL 而言,我可能不是 100% 确定如何设置一般的访问控制,但是我非常有信心这不是问题,因为我也尝试过执行我的使用控制台中的 Cloud Storage API READ 和 WRITE 访问权限配置 Compute Engine VM 后,在 Compute Engine VM 上编写脚本。

此外,当您收到 502 Bad Gateway 响应时,我知道文档建议立即重试(使用“指数回退”),但我现在每次尝试几个小时时都会收到此错误。所以,要么是谷歌存储服务在某些方面坏了,要么我的客户端坏了,对吧?更进一步,我可以使用gsutil cp ... 在命令行上推送 jpeg 文件,这让我觉得我坏了而不是谷歌,并且存储桶可以使用我的默认凭据写入。听起来不错?

这是我非常简单的客户端脚本,它是来自this 官方示例的陪审团操纵:

import json
from apiclient import discovery
from apiclient.http import MediaFileUpload
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('storage', 'v1', credentials=credentials)
image = MediaFileUpload('up.jpg', mimetype='image/jpeg')
req = service.objects().insert_media(bucket='somebucket', media_body=image)
resp = req.execute()
print(json.dumps(resp, indent=2))

这是我得到的踪迹:

Traceback (most recent call last):
  File "upload.py", line 10, in <module>
    resp = req.execute()
  File "/Envs/py2.7/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Envs/py2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 729, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 502 when requesting https://www.googleapis.com/upload/storage/v1/b/somebucket/o?uploadType=media&alt=media returned "Bad Gateway">

我已经尝试过 python 2.7 和 python 3.5,所以我认为这不是问题(文档告诉我 google python 客户端在 3.5 中可以正常工作,但没有经过很好的测试)。此外,根据here,我已经尝试了一个“可恢复上传”变体,我得到了相同的、永远忠实的 502 Bad Gateway。谷歌坏了吗?请帮忙。

【问题讨论】:

    标签: python google-cloud-storage google-compute-engine google-client


    【解决方案1】:

    您需要为您的对象指定一个目标名称。我相信您想要方法service.objects().insert() 而不是service.objects().insert_media()。所以整行是:

    req = client.objects().insert(
        bucket='somebucket',
        name='nameOfObject',
        media_body=image)
    

    objects.insert 文档页面上有a complete example

    不过,我不确定您为什么会收到 502。这可能是一个错误。

    【讨论】:

    • 就是这样!非常感谢。
    猜你喜欢
    • 2015-02-18
    • 2015-03-01
    • 2021-06-15
    • 1970-01-01
    • 2016-12-16
    • 2013-05-07
    • 2017-12-06
    • 2018-04-17
    • 2019-02-01
    相关资源
    最近更新 更多