Backblaze Python SDK v2 - 从内存提供文件响应

Backblaze Python SDK v2 - serve file response from memory

我希望 Backblaze 上私有存储桶中的一些文件(在本例中为图像)由 Flask 中的 API 端点公开。我不确定如何处理从 bucket.download_file_by_name

返回的 DownloadedFile 对象
@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 returns 一个 DownloadedFile 对象,我不知道如何处理它。该文档没有提供示例,似乎暗示我应该做类似的事情:

file = bucket.download_file_by_name(filename)

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

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

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

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

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

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

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