AuthorizationError:期望来自Google Storage的状态[201]。但得到状态401

时间:2017-06-12 19:22:29

标签: google-cloud-platform google-cloud-storage

我正试图通过" App Engine"在谷歌云存储中创建一个新文件。我已经为存储桶的服务帐户授予了完全权限,但仍然收到以下错误。

代码:

import cloudstorage as _gcs

from google.appengine.api import app_identity,urlfetch

class get_ad_insights(webapp.RequestHandler):
    def get(self):
        bucket_name = 'awstogcs'
        bucket = '/' + bucket_name
        filename = bucket + '/facebook_data'
        self.tmp_filenames_to_clean_up = []

        write_retry_params = _gcs.RetryParams(backoff_factor=1.1)
        gcs_file=_gcs.open(
            filename, 'w', content_type='text/plain', options={
                'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar'},
                 retry_params=write_retry_params) 
        gcs_file.write('abcd\n')
        gcs_file.write('f'*1024*4 + '\n')
        gcs_file.close()
        self.tmp_filenames_to_clean_up.append(filename)

错误讯息:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "C:\Chaitu\Projects\fb\controllers\getinsights.py", line 40, in get
    retry_params=write_retry_params)
  File "lib\cloudstorage\cloudstorage_api.py", line 95, in open
    return storage_api.StreamingBuffer(api, filename, content_type, options)
  File "lib\cloudstorage\storage_api.py", line 699, in __init__
    body=content)
  File "lib\cloudstorage\errors.py", line 128, in check_status
    raise AuthorizationError(msg)
**AuthorizationError: Expect status [201] from Google Storage. But got status 401.**
Path: '/media-appengine/facebook_data'.
Request headers: {'content-type': 'text/plain', 'x-goog-meta-foo': 'foo', 'x-goog-api-version': '2', 'x-goog-resumable': 'start', 'x-goog-meta-bar': 'bar', 'accept-encoding': 'gzip, *'}.
Response headers: {'alt-svc': 'quic=":443"; ma=2592000; v="38,37,36,35"', 'content-type': 'application/xml; charset=UTF-8', 'date': 'Mon, 12 Jun 2017 19:17:37 GMT', 'www-authenticate': 'Bearer realm="https://accounts.google.com/", error="invalid_token"', 'content-length': '131', 'x-guploader-uploadid': 'AEnB2Uo4r-8lsPGFCJNsbukPIHyX1eHTS9pC9cMOmDFqwPr_8ERTdBpwa8ce1T5ri5Y5OaJ3Qx8ZRYVM8QwT1ImL5fjmsZguUg', 'vary': 'Origin', 'server': 'UploadServer'}.
Body: "<?xml version='1.0' encoding='UTF-8'?><Error><Code>AuthenticationRequired</Code><Message>Authentication required.</Message></Error>".
Extra info: None.

1 个答案:

答案 0 :(得分:0)

当我删除以下代码选项= {&#39; x-goog-meta-foo&#39;:&#39; foo&#39;,&#39; x-goog-meta-bar&#39; :&#39; bar&#39;}脚本正在运行