【发布时间】:2021-01-07 16:31:59
【问题描述】:
目前我正在尝试允许用户通过我的 python API 将个人资料图片上传到 Firestore 存储桶。我正在尝试以请求的表单数据发送图像。但是,当我尝试上传图片时,出现以下错误, “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 吗?
【问题讨论】:
标签: python flask google-cloud-storage