images.get_serving_url GCS 图像 blob 失败

images.get_serving_url failing for GCS image blobs

我有一个较旧的 (Python 2.7) Google App Engine 应用程序,我最近使用 Google 云平台 (gcloud) 部署了它,因为 appcfg 已停用。我在使用 images.get_serving_url 时遇到问题,不适用于上传的图像 blob。图片 API 返回 'TransformationError'。在切换到 google 云平台之前,此代码在生产环境中工作了多年。

需要注意的一点是,Google Cloud Platform 为 blob 创建了一个默认存储桶。以前,我认为 GAE 使用的是 Blobstore,所以尽管这种变化对开发人员来说是透明的,但可能在幕后发生了很多事情。我认为出于某种原因,我的应用在为存储在云存储桶中的文件创建图像 url 时遇到问题。

我做了一个非常简单的测试用例来重现这个问题。是否有新的要求,或者这应该行得通吗?

class testUpload(webapp.RequestHandler):
    def get(self):
        uploadUrl = blobstore.create_upload_url('/testUploadHandler')
        print("Generated upload url: %s" % uploadUrl)

        self.response.out.write("""
            <html><body>
            <form action="{0}" method="POST" enctype="multipart/form-data">
              Upload File: <input type="file" name="file"><br>
              <input type="submit" name="submit" value="Submit">
            </form>
            </body></html>""".format(uploadUrl))
        

class testUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        
        logging.info("testUploadHandler called.")
        upload = self.get_uploads()[0]
        blob_key = upload.key()
        image_url = images.get_serving_url(blob_key)
        self.redirect(image_url) 

部署的代码returns Web 服务器上出现以下错误(本地开发服务器正常):

Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~puzzleflow-dev/20210325t013659.433969250928848570/puzzleflow.py", line 4171, in post
    image_url = images.get_serving_url(blob_key)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/1/google/appengine/api/images/__init__.py", line 1866, in get_serving_url
    return rpc.get_result()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 615, in get_result
    return self.__get_result_hook(self)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/fdc6d631da52d25b/python27/python27_lib/versions/1/google/appengine/api/images/__init__.py", line 1970, in get_serving_url_hook
    raise _ToImagesError(e, readable_blob_key)
TransformationError

另外,你可以自己尝试这个link(它应该是公开的):

https://puzzleflow-dev.appspot.com/testUpload

问题是我已将访问控制切换为统一,因为 Google 说这是 'recommended' 设置。我不太了解 Fine-Grained 和 Uniform 之间的区别,但是将其切换回 Fine-Grained 解决了这个问题。