1

我希望 Backblaze 上的私有存储桶中的一些文件(在这种情况下为图像)由 Flask 中的 API 端点公开。我不确定如何处理DownloadedFile从返回的对象bucket.download_file_by_name

@app.route('/b2-image/<filename>', methods=('GET',))
def b2_image(filename):
    info = InMemoryAccountInfo()
    b2_api = B2Api(info)
    app_key_id = env['MY_KEY_ID']
    app_key = env['MY_KEY']

    b2_api.authorize_account('production', app_key_id, app_key)
    bucket = b2_api.get_bucket_by_name(env['MY_BUCKET'])

    file = bucket.download_file_by_name(filename)

    bytes = BytesIO()

    #something sort of like this??? 
    #return Response(bytes.read(file), mimetype='image/jpeg')
    

bucket.download_file_by_name返回一个DownloadedFile对象,我不知道如何处理它。该文档没有提供任何示例,似乎建议我应该执行以下操作:

file = bucket.download_file_by_name(filename)

#this obviously doesn't make any sense
image_file = file.save(file, file)
4

1 回答 1

1

我一直在尝试以下方法,但没有奏效:

file = bucket.download_file_by_name(filename)
f = BytesIO()
file.save(f)

return Response(f.read(), mimetype='image/jpeg')

这个 SO Answer给了我需要的线索,特别是在对象上设置seek值:BytesIO

#this works
file = bucket.download_file_by_name(filename)
f = BytesIO()
file.save(f)
f.seek(0)

return Response(f.read(), mimetype='image/jpeg')
于 2022-02-10T19:09:23.450 回答