gpt4 book ai didi

python - 尝试从 GAE python App 将文件写入 GCS 时出现 ForbiddenError

转载 作者:行者123 更新时间:2023-11-28 19:33:28 26 4
gpt4 key购买 nike

我有这个代码:

def save_to_gcs(self, img, img_obj):
'''
Image data, Image metadata object -> Blob Key
Given an image and image metadata, stores it in a GCS bucket
'''
bucket = '/foo'
filename = bucket + '/' + str(img_obj['filename'])

self.tmp_filenames_to_clean_up = []

logging.info('Creating file %s\n' % img_obj['filename'])

write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open(filename,
'w',
content_type=img_obj['mimetype'],
retry_params=write_retry_params)
gcs_file.write(img)
gcs_file.close()
self.tmp_filenames_to_clean_up.append(filename)

return blobstore.create_gs_key('/gs/' + filename)

但它失败并出现此错误:

Expect status [201] from Google Storage. But got status 403. Response headers: {'content-length': '145', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'server': 'HTTP Upload Server Built on Jun 7 2013 11:30:13 (1370629813)', 'pragma': 'no-cache', 'cache-control': 'no-cache, no-store, must-revalidate', 'date': 'Thu, 20 Jun 2013 23:13:55 GMT', 'content-type': 'application/xml; charset=UTF-8'}
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~foo/5.368231578716365248/main.py", line 409, in post
blob_key = self.save_to_gcs(img, img_obj) # Save the image to a GCS bucket. returns a blob_key
File "/base/data/home/apps/s~foo/5.368231578716365248/main.py", line 448, in save_to_gcs
retry_params=write_retry_params)
File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/cloudstorage_api.py", line 69, in open
return storage_api.StreamingBuffer(api, filename, content_type, options)
File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/storage_api.py", line 527, in __init__
errors.check_status(status, [201], headers)
File "/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/errors.py", line 99, in check_status
raise ForbiddenError(msg)
ForbiddenError: Expect status [201] from Google Storage. But got status 403. Response headers: {'content-length': '145', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'server': 'HTTP Upload Server Built on Jun 7 2013 11:30:13 (1370629813)', 'pragma': 'no-cache', 'cache-control': 'no-cache, no-store, must-revalidate', 'date': 'Thu, 20 Jun 2013 23:13:55 GMT', 'content-type': 'application/xml; charset=UTF-8'}

如果您能帮助破译该错误并提出解决方案,我们将不胜感激。

谢谢

最佳答案

该文档对于授予对您的 App Engine 应用程序的访问权限感到困惑(这很可能是您的问题)。这是最新的 Google Cloud Console 对我有用的东西:

  1. 在 Google Cloud Console 中,点击您的项目,然后点击“APIs & auth”。打开“Google Cloud Storage”和“Google Cloud Storage JSON API”。
  2. 点击返回“概览”屏幕,然后点击左侧菜单中的“云存储”。
  3. 点击存储桶旁边的复选框,然后点击“存储桶权限”按钮。
  4. 添加新的“用户”权限并指定您的应用引擎服务帐户名称,格式为 application-id@appspot.gserviceaccount.com。这可以在 AppEngine 控制台的应用程序设置中找到。查找此帐户名称的更好说明是 here .
  5. 保存您的更改。

关于python - 尝试从 GAE python App 将文件写入 GCS 时出现 ForbiddenError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17225638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com