为什么图像属性没有上传到gcs的方法文件名?

2024-06-30 15:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python flask将图像上载到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”],并再次遵循上述文档。现在我得到一个新的错误:-

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

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


Tags: 代码文档imageform表单databuckettype