Python 如何将图像上载到Google Firestore Bucket
目前,我正在尝试允许用户通过我的python API将配置文件图片上传到firestore bucket。我正在尝试以请求的形式发送图像。但是,当我尝试上传图像时,我得到以下错误:, “TypeError:无法在类似字节的对象上使用字符串模式” 这是:Python 如何将图像上载到Google Firestore Bucket,python,flask,google-cloud-storage,Python,Flask,Google Cloud Storage,目前,我正在尝试允许用户通过我的python API将配置文件图片上传到firestore bucket。我正在尝试以请求的形式发送图像。但是,当我尝试上传图像时,我得到以下错误:, “TypeError:无法在类似字节的对象上使用字符串模式” 这是: @users_endpoints.route('/addProfilePicture', methods=['POST']) def add_profile_picture(): request_data = request.form['
@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格式?您可以将
内容\u type
选项传递给
作为的实例的imagefile具有可从中获取此值的content\u type
属性
image\u blob.upload\u from\u文件(
imagefile.stream,
content\u type=imagefile.content\u type
)