如何将图片上传到Google Firestore Bucket



目前我正试图允许用户通过我的python API将个人资料图片上传到消防存储桶。我正在尝试以请求的形式数据发送图像。然而,当我尝试上传图像时,我会出现以下错误,"TypeError:不能在类似字节的对象"上使用字符串模式;这是:

@users_endpoints.route('/addProfilePicture', methods=['POST'])
def add_profile_picture():
request_data = request.form['some_text']
print(request_data)
imagefile = request.files.get('imagefile', False)
# imagefile.save('/Users/joeytrasatti/Moove/test.jpg')
# auth_token = body.get('token')
# if not auth_token:
#     return no_auth_token_response()
# uid, utype = auth.decode_auth_token(auth_token)
image_blob = bucket.blob(f'users/test')
image_blob.upload_from_filename(imagefile.stream.read())
return make_response(jsonify(True)), 200

我可以将文件保存到我的计算机上,并且我可以上传保存的文件,我只是不能直接从请求上传文件。我尝试使用解码请求

image_blob.upload_from_filename(imagefile.stream.read().decode('utf-8')

但我随后得到了以下错误:UnicodeDecodeError:"utf-8"编解码器无法解码位置0中的字节0x89:无效的起始字节如有任何帮助,我们将不胜感激!

编辑:我可以通过使用这一行而不是以前的上传行来让它更接近我想要的!

image_blob.upload_from_file(imagefile.stream)

但是,它被存储为应用程序/八位字节流文件。我可以把它存储为jpeg吗?

您可以将content_type选项传递给google.cloud.storage.blob.Blob.upload_from_file

作为werkzeug.datastructures.FileStorage实例的imagefile具有content_type属性,您可以从中获取该值。

image_blob.upload_from_file(
imagefile.stream,
content_type=imagefile.content_type
)

最新更新