【问题标题】:Why the image attribute doesn't have method filename to upload to gcs?为什么图像属性没有方法文件名上传到 gcs?
【发布时间】:2021-07-01 23:53:33
【问题描述】:

我正在尝试使用 python 烧瓶将图像上传到 gcs。我已经参考了谷歌提供的文档,https://cloud.google.com/appengine/docs/flexible/python/using-cloud-storage,但我得到了一个错误:-

AttributeError: 'str' 对象没有属性 'filename'

我已经完全按照文档中的文档进行了操作,但仍然出现这些错误,我不知道为什么。

我的html表单:-

<form action="/register", method="POST" ,enctype="multipart/form-data">
 <input type="file" , name="image" , placeholder="Image">
</form>

我的python烧瓶代码:-

#fetch user image
    userImage = request.files["image"]
    #upload userImage
    #create storageclient
    gcs = storage.Client.from_service_account_json("projectalpha24-e0c2991ff2c0.json")
    #get storage bucket
    bucket = gcs.get_bucket("projectalpha24.appspot.com")
    #create a blob and upload file content
    blob = bucket.blob(userImage.filename)

    blob.upload_from_string(
        userImage.read(),
        content_type=userImage.content_type
    )

我不确定发生了什么。任何帮助将不胜感激。

编辑:我已经编辑了代码。我已将从表单中获取图像的方式更改为 request.files["image"] 并再次遵循上述文档 1 次。现在我得到一个新错误:-

请求的 mimetype 是“application/x-www-form-urlencoded”而不是“multipart/form-data”,这意味着没有传输任何文件内容。要修复此错误,您应该在表单中提供 enctype="multipart/form-data"

虽然,正如您从我的表单代码中看到的那样,我确实在表单中包含了 enctype,但是,它给出了这个错误。

【问题讨论】:

  • 您的错误来自这一行吗? userImage.filename(userImage.filename)

标签: python google-app-engine flask google-cloud-platform google-cloud-storage


【解决方案1】:

需要通过userImage = request.files.get("image")获取文件对象。更多信息请参考this link

【讨论】:

  • 我实际上在我的足迹中使用过这个,但仍然没有太大的不同。
猜你喜欢
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多