从App Engine(端点)将文件上载到GCS。属性类型错误:

时间:2014-11-25 21:03:24

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

我尝试从Appengine端点向GCS上传文件。我使用的是Python。当文件结束上传时,显示错误" 属性错误:' str'对象没有属性' 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端点正确地将文件上传到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'

1 个答案:

答案 0 :(得分:0)

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

当你(代码中的某个地方)为其中一个属性分配一个字符串值时,proto模型或者端点proto模型表现得很奇怪。当它试图将它转换为Message(并因此递归地将其属性转换为Messages)时,它会找到String并发出错误。没有看到受影响的端点方法的代码就很难分辨。


更新:此外,检查错误行上方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.

这适用于你吗?