【问题标题】:Upload file to GCS from Appengine (Endpoints). AttributeTypeError:将文件从 App Engine(端点)上传到 GCS。属性类型错误:
【发布时间】:2015-01-24 00:59:25
【问题描述】:

我正在尝试将文件从 Appengine Endpoints 上传到 GCS。我正在使用 Python。当文件结束上传时,显示错误“AttributeError: 'str' object has no attribute 'ToMessage'”。

所以,如果我转到 GCS 文件资源管理器,在浏览器中,我会看到最近上传的文件名,但其大小为 0K。

这是我的模型:

class File(EndpointsModel):
    _message_fields_schema = ('blob', 'url')

    blob = ndb.BlobKeyProperty() #stored in GCS
    url = ndb.StringProperty()
    enable = ndb.BooleanProperty(default=True)

    def create_file(filename):
        file_info = blobstore.FileInfo(filename)
        filename = '/gs'+ str(file_info.filename.blob)

        gcs.open(secrets.BUCKET_NAME +'/' + filename, 'w').close()
        return blobstore.create_gs_key(filename)

那么,我需要做些什么才能将文件从 Appengine Endpoints 正确上传到 GCS。

追溯:

ERROR    2014-11-25 20:35:22,654 service.py:191] Encountered unexpected error from ProtoRPC method implementation: AttributeError ('str' object has no attribute 'ToMessage')
Traceback (most recent call last):
File "/home/alpocr/workspace/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
response = method(instance, request)
 File "/home/alpocr/workspace/google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
return remote_method(service_instance, request)
File "/home/alpocr/workspace/google_appengine/lib/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
response = method(service_instance, request)
File "/home/alpocr/workspace/mall4g-backend/libs/endpoints_proto_datastore/ndb/model.py", line 1429, in EntityToRequestMethod
response = response.ToMessage(fields=response_fields)
AttributeError: 'str' object has no attribute 'ToMessage'

【问题讨论】:

  • 请包含错误的完整回溯。
  • 我已经用回溯更新了帖子。

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


【解决方案1】:

听起来您已经为端点方法正确定义了返回类型,并且期望将结果转换为 Message 对象,但端点方法代码实际上返回的是一个字符串。可以贴一下出现这个错误时调用的endpoints方法吗?

当您(在代码中的某处)将字符串值分配给其属性之一时,该模型或端点原型模型都表现得很奇怪。当它尝试将其转换为消息(并因此递归地将其属性转换为消息)时,它会找到字符串并找出错误。没有看到受影响的端点方法的代码就很难判断。


更新:另外,检查错误所在行上方的endpoints_proto_datastorewe see the following comment 的来源:

# If developers using a custom request message class with
# response_fields to create a response message class for them, it is
# up to them to return an instance of the current EndpointsModel
# class. If not, their API users will receive a 503 from an uncaught
# exception.

这适用于您吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多