繁体   English   中英

从Appengine(端点)将文件上传到GCS。 AttributeTypeError:

[英]Upload file to GCS from Appengine (Endpoints). AttributeTypeError:

我正在尝试从Appengine端点将文件上传到GCS。 我正在使用Python。 文件结束上传后,显示错误“ AttributeError:'str'对象没有属性'ToMessage' “。

因此,如果我转到GCS File Explorer,在浏览器中,我会看到最近上传的文件名,但文件大小为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'

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

当您(在代码中的某个位置)将字符串值分配给其属性之一时,这两个端点原型模型都表现得很奇怪。 当它尝试将其转换为Message(从而将其属性递归转换为Messages)时,它会找到String并发现错误。 不看受影响的终结点方法的代码就很难分辨。


更新:另外,检查endpoints_proto_datastore的来源, 我们在出现错误的代码行上方看到以下注释

# 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.

这对您适用吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM