GAE无法在开发环境中使用cloudstorage

时间:2018-04-09 16:54:52

标签: python google-app-engine google-cloud-storage

我正在使用GAE的dev版本上传一些zip文件,解压缩并在屏幕上编辑它们,截至目前我只是想看看文件是否可以在上传和解压缩后打开。 / p>

如果我尝试打开任何其他地方将它们放在后台任务上,我会收到以下错误:

Traceback (most recent call last):
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
    req.respond()
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
    self.server.gateway(self).respond()
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 292, in __call__
    return app(environ, start_response)
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
    response_body = iter(application(environ, wrapped_start_response))
  File "/home/ricardo/workspace/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/request_handler.py", line 148, in __call__
    environ[http_runtime_constants.REQUEST_ID_ENVIRON])
KeyError: 'HTTP_X_APPENGINE_DEV_REQUEST_ID'

以下是代码:

class ExportConfiguration(blobstore_handlers.BlobstoreUploadHandler):
def post(self, store_id):
    # upload_files = self.get_uploads('file')
    bucket = '/' + BUCKET
    upload = self.get_uploads()[0]
    # write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1)
    file = zipfile.ZipFile(upload.open())
    # logging.info(file)
    for name in file.namelist():
        outfile = file.read(name)
        outfile_list = name.split('/')
        for idx, path in enumerate(outfile_list):
            if path == 'bundles':
                node = outfile_list[idx - 1]
                outfile_path = 'data/{}/{}'.format(node, '/'.join(outfile_list[idx:]))
        params = {'path': '{}/{}'.format(bucket, outfile_path), 'file': outfile}

        task = Task(url='/tasks/write_loaders/',
                    params=params,
                    method='POST',
                    target='background-tasks')
        task.add()
        # outfile_full = '{}/{}'.format(bucket, outfile_path)
        # logging.info(outfile_full)
        # gcs_file = cloudstorage.open(outfile_full, 'w', options={'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar'}, retry_params=write_retry_params)
        # gcs_file.write('outfile')
        # gcs_file.close()
    # file.extract()
    # file = str(self.request.POST.get(' filename'))
    # open(str(self.request.POST.get(' filename')).split('"')[1])
    # blob_info = upload_files[0]
    stats = cloudstorage.listbucket(bucket)
    for s in stats:
        logging.info(s.filename)
    logging.info(dir(stats))
    resp = {'code': 0, 'result': 'Success'}
    self.response.headers['Content-Type'] = 'application/json'
    self.response.out.write(json.dumps(resp, separators=(',', ':')))


class LoaderWriter(webapp2.RequestHandler):
    def post(self):
        path = self.request.POST.get('path')
        file = self.request.POST.get('file')
        file = file.encode('utf-8')
        file = etree.XML(file)
        gcs_file = cloudstorage.open(path, 'w')
        gcs_file.write(etree.tostring(file))
        gcs_file.close()

我试着看看这是否是一个gcs配置问题,无济于事,还有其他人遇到过同样的问题吗?

2 个答案:

答案 0 :(得分:1)

问题似乎与你正在扩展BlobstoreUploadHandler有关。显然,此处理程序会删除HTTP_X_APPENGINE_DEV_REQUEST_ID之类的http标头,这些标头是gcs库执行的进一步API调用所必需的。

如果您尝试从常规处理程序webapp2.RequestHandler打开该文件,它应该可以正常工作。

答案 1 :(得分:0)

基于@ Peuchele答案的潜在方法。

在我的情况下,通过使用双重继承,我能够解决由blobstore_handlers.BlobstoreUploadHandlerwebapp2.RequestHandler之间的差异引起的类似问题。类似的东西:

class ExportConfiguration(webapp2.RequestHandler,
                          blobstore_handlers.BlobstoreUploadHandler):

或(继承顺序可能很重要):

class ExportConfiguration(blobstore_handlers.BlobstoreUploadHandler,
                          webapp2.RequestHandler):

请注意,此方法可能在所有情况下都不起作用,具体取决于处理程序的实现。