Python 为什么图像属性没有上传到gcs的方法文件名?
我正在尝试使用python flask将图像上载到gcs。我参考了谷歌提供的文档,https://cloud.google.com/appengine/docs/flexible/python/using-cloud-storage,但我得到一个错误:- AttributeError:“str”对象没有属性“filename” 我已经精确地跟踪了文档中的文档,但是我仍然得到这些错误,我不知道为什么 我的html表单:-Python 为什么图像属性没有上传到gcs的方法文件名?,python,google-app-engine,flask,google-cloud-platform,google-cloud-storage,Python,Google App Engine,Flask,Google Cloud Platform,Google Cloud Storage,我正在尝试使用python flask将图像上载到gcs。我参考了谷歌提供的文档,https://cloud.google.com/appengine/docs/flexible/python/using-cloud-storage,但我得到一个错误:- AttributeError:“str”对象没有属性“filename” 我已经精确地跟踪了文档中的文档,但是我仍然得到这些错误,我不知道为什么 我的html表单:- <form action="/register",
<form action="/register", method="POST" ,enctype="multipart/form-data">
<input type="file" , name="image" , placeholder="Image">
</form>
我不知道发生了什么事。任何帮助都将不胜感激
编辑:我已经编辑了代码。我已将从表单获取图像的方式更改为request.files[image],并再次遵循上述文档。现在我得到一个新错误:-
请求的mimetype是application/x-www-form-urlencoded,而不是multipart/form数据,这意味着没有传输任何文件内容。要修复此错误,应在表单中提供enctype=multipart/form数据
尽管从我的表单代码中可以看到,我确实在表单中包含了enctype,但它给出了这个错误。您需要通过userImage=request.files.getimage获取文件对象。有关更多信息,请参阅。您的错误是否来自此行?实际上,在我的研究过程中,你使用过这个,但仍然没有什么不同。
#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
)