温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Google Images API's execute_transforms() returning "there has been an error"
google-app-engine google-cloud-storage image python

python - Google Images API的execute_transforms()返回“出现错误”

发布于 2020-04-08 00:03:04

我正在尝试使用Google的图像处理API。看起来非常简单,但是当我调用execute_transformsImage对象时,它会抛出一个通用的“出现了错误”

gcs_file_location是正确的,没有错误抛出,直到我叫execute_transforms()。图像文件不是太大。

from google.appengine.api import images


gcs_file_location = '/gs/' + gcs_file_location
img = images.Image(filename=gcs_file_location)
img.resize(width=50, height=50)

# The following code throws "There has been an error"
thumbnail = img.execute_transforms()    

查看更多

提问者
Antoine Vo
被浏览
81
Pol Ortiz 2019-04-24 20:44

我花了一段时间,但是我设法复制了您的方案,因此我设置了Python 2.7环境(根据Images Python API Overview的教程,并使用官方存储库中的代码设置Django所需的所有文件并运行它在Django 官方教程的帮助下

然后,我修改了服务轮询,以执行引发这些错误的功能。

def index(request):
    filename_gs=request.GET.get("id")
    img = images.Image(filename=filename_gs)
    img.resize(width=1000, height=1000)
    thumbnail = img.execute_transforms()

而且我发现确实您是对的,在函数中execute_transforms引发错误并停止执行。但是,我没有您要参考的一般错误,我得到以下信息:

  File "/home/polortiz/DJANGOOO/mytry3/python-docs-samples/appengine/standard/django/polls/views.py", line 48, in index
    thumbnail = img.execute_transforms()
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/api/images/__init__.py", line 810, in execute_transforms
    return rpc.get_result()
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 615, in get_result
    return self.__get_result_hook(self)
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/api/images/__init__.py", line 898, in execute_transforms_hook
    raise _ToImagesError(e, self._blob_key)
TransformationError

我进行了进一步调查,以检查可能是引起这些错误的问题,并找到了该函数库代码在那里,您可以看到引发的所有异常:

  • BadRequestError:当请求规范无效时。
  • NotImageError:当给定的图像数据不是图像时。
  • BadImageError:当给定的图像数据损坏时。
  • LargeImageError:给定的图像数据太大而无法处理时。
  • InvalidBlobKeyError:提供的Blob键无效时。
  • TransformationError:图像处理期间发生错误时。
  • AccessDeniedError:当Blob键指向Google存储对象,并且应用程序无权访问该对象时。
  • ObjectNotFoundError:当Blob键指向不再存在的对象时。

因此,为了解决您的问题,最好检查所有这些点。回答评论中提出的问题:

django是否与Google的Imaging API不兼容,还是需要导入WebApp2?

Google的Imaging API与Django兼容。使用WebApp2来处理http请求和响应不是强制性的。