【发布时间】:2022-01-22 14:47:35
【问题描述】:
我在使用烧瓶 + 天蓝色应用时遇到问题。我在存储中保存了一些文件(pdf 和 html),我需要在调用 get_file_safe 端点时返回这些文件。此方法采用 file_id 参数并访问数据库,转到 blob azure,创建一个临时文件并返回该文件。当我传递引用 PDF 文件的代码时,它可以完美运行,并且文件显示在屏幕上。当代码与 HTML 文件匹配时,答案为空白。有谁知道它可能是什么?非常感谢 ! (注意:当我使用 GCP 时它可以工作,但我必须迁移,所以我在这里表示它是 azure)。
from flask import Flask, flash, jsonify, session, redirect, url_for, escape, request, render_template, session, send_file
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__, ContentSettings
def get_file_safe():
#login and security stuff (...) Logic goes here ->>>
file_id = request.args.get('file_id')
cursor.execute(
"""SELECT link, mimetype from TABLE where id = %s """, (file_id))
rows = cursor.fetchall()
link = rows[0][0]
mimetype = rows[0][1]
filename = link.split("/")[-1]
print("Filename{}".format(filename))
print("Mimetype {}".format(mimetype))
# google cloud version, commented
#client = storage.Client()
#bucket = client.get_bucket('BUCKET_NAME')
#blob = bucket.blob(link)
#with tempfile.NamedTemporaryFile() as temp:
# blob.download_to_filename(temp.name)
# return send_file(temp.name, attachment_filename=filename)
# azure verson
bucket_name = 'BUCKET-NAME'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(container=bucket_name, blob=link)
with tempfile.NamedTemporaryFile() as temp:
temp.write(blob_client.download_blob().readall())
#return send_file(temp.name, attachment_filename=filename, mimetype=mimetype)
return send_file(temp.name, download_name=filename)
【问题讨论】:
标签: python flask azure-blob-storage