本文介绍了尝试从GAE python App向GCS写入文件时发生ForbiddenError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:
$ b $ pre $ def $ to $ g
图像数据,图像元数据对象 - > Blob Key
给定图像和图像元数据,将其存储在GCS桶中
'''
bucket ='/ foo'
filename = bucket +'/'+ str img_obj ['filename'])

self.tmp_filenames_to_clean_up = []

logging.info('Creating file%s\\\
'%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(文件名)

返回blobstore .create_gs_key('/ gs /'+ filename)

但是失败并出现此错误:

 预计Google Storage的状态[201]。但状态为403.响应标题:{'content-length':'145','via':'HTTP / 1.1 GWA','x-google-cache-control':'remote-fetch','expires': 'Fri,1990年1月1日00:00:00 GMT','服务器':'HTTP上传服务器构建于2013年6月7日11:30:13(1370629813)','编译指示':'无缓存','控制':'no-cache,no-store,must-revalidate','date':'Thu,20 Jun 2013 23:13:55 GMT','content-type':'application / xml; charset = UTF-8'} 
Traceback(最近一次调用最后一次):
文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1536行,在__call__中
rv = self.handle_exception(request,response,e)
在__call__ $ b $中的文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1530行b rv = self.router.dispatch(请求,响应)
文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1278行,在default_dispatcher
返回路由.handler_adapter(request,response)
文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,行1102,在__call__
return handler.dispatch()
文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第572行,发送
返回self.handle_exception(e,self.app.debug)
文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,行570,分派
返回方法(* args,** kwargs)
文件/base/data/home/apps/s~foo/5.368231578716365248/main.py,第409行,后
blob_key = self.save_to_gcs(img,img_obj)#将图像保存到GCS存储桶。返回一个blob_key
文件/base/data/home/apps/s~foo/5.368231578716365248/main.py,第448行,在save_to_gcs
retry_params = write_retry_params)
文件/ base /data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/cloudstorage_api.py,第69行,打开
返回storage_api.StreamingBuffer(api,filename,content_type,options)
文件 /base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/storage_api.py,第527行,在__init__
errors.check_status(status,[201],headers)
文件/base/data/home/apps/s~foo/5.368231578716365248/external/cloudstorage/errors.py,第99行,在check_status中
raise ForbiddenError(msg)
ForbiddenError:期望状态[201]来自Google存储。但状态为403.响应标题:{'content-length':'145','via':'HTTP / 1.1 GWA','x-google-cache-control':'remote-fetch','expires': 'Fri,1990年1月1日00:00:00 GMT','服务器':'HTTP上传服务器构建于2013年6月7日11:30:13(1370629813)','编译指示':'无缓存','控制':'no-cache,no-store,must-revalidate','date':'Thu,20 Jun 2013 23:13:55 GMT','content-type':'application / xml; charset = UTF-8'}

解密这个错误并提出解决方案的任何帮助将会是非常感谢。



谢谢

解决方案

它让我感到困惑。按照先决条件部分中的上的步骤操作,我就可以开始工作。尽管有一些注意事项:




  • 对于2号码,请确保转到并在服务中打开GCS

  • 对于数字5,请转至,选择您的项目,单击设置扳手并单击小组。在这里添加你的gserviceaccount.com。

  • 对于数字5,我认为你必须用gsutil编辑ACL文件。按照提供的替代说明进行操作。



这对你来说应该是有效的,因为它适合我。


I have this code:

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)

But it fails with this error:

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'}

Any help with deciphering that error and coming up with a solution would be much appreciated.

Thanks

解决方案

Same thing happened to me and it baffled me. I got it working by following the steps on this page under the Prerequisites section. A couple notes though:

  • For number 2, make sure you go to the APIs Console and turn on GCS under Services
  • For number 5, go to the Cloud Console, select your project, click the Settings wrench and click Teams. Add your gserviceaccount.com thing here.
  • Also for number 5, I think you have to edit the ACL files with gsutil. Follow the alternate instructions provided.

That should work for you since it did for me.

这篇关于尝试从GAE python App向GCS写入文件时发生ForbiddenError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 02:41