如何使用 python flask 创建下载 API 以下载文件
How to create download API to download files using python flask
我有一个 python 代码,可以从 azure blob 存储中下载 url 图像文件。如果我们在浏览器中复制粘贴此 url,它只会下载文件。我必须创建一个下载 api ,调用它时只会下载文件。以下是我目前拥有的代码:
from urllib.request import urlopen
from flask import Flask, jsonify
import uuid
from flask_cors import CORS
from datetime import datetime
from azure.storage.blob import generate_blob_sas, AccountSasPermissions
import datetime
from datetime import timedelta
app = Flask(__name__)
CORS(app)
uid_secret_key = str(uuid.uuid4())
app.secret_key = uid_secret_key
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app
@app.route('/download')
def download():
account_name = "deeusblobstorage"
container_name = "rperodct"
blob_name = "face_1_7285.jpg"
account_key = "hMlIRXCjAomfDRfied8Y5FPwTVnWEkEDuVsw//CQtkiOdcFD/y5wUcxH9Ou7Ni+DtDQFwe23YbZ3Qia9gw=="
url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"
sas_token = generate_blob_sas(
account_name=account_name,
account_key=account_key,
container_name=container_name,
blob_name=blob_name,
permission=AccountSasPermissions(read=True),
expiry=datetime.datetime.utcnow() + timedelta(hours=1)
)
url_with_sas = f"{url}?{sas_token}"
print(url_with_sas)
urlopen(url_with_sas)
@app.route('/')
def hello_world():
return jsonify({'Status': 'ok', 'date': '05.25.21'}), 200
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)
/download
api 包含从变量名 url_with_sas
中的 azure 容器下载 url blob 的代码。现在我想以这样的方式制作这个 API,如果我们点击它,它应该自动从 url 下载文件。所以为此我想到了添加 urlopen(url_with_sas)
这将打开 url。但似乎它不起作用。任何人都可以为此提出一些建议。谢谢
如评论中所述,解决方案是通过 return redirect(url_with_sas)
所以整个函数看起来像这样:
@app.route('/download')
def download():
account_name = "deeusblobstorage"
container_name = "rperodct"
blob_name = "face_1_7285.jpg"
account_key = "hMlIRXCjAomfDRfied8Y5FPwTVnWEkEDuVsw//CQtkiOdcFD/y5wUcxH9Ou7Ni+DtDQFwe23YbZ3Qia9gw=="
url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"
sas_token = generate_blob_sas(
account_name=account_name,
account_key=account_key,
container_name=container_name,
blob_name=blob_name,
permission=AccountSasPermissions(read=True),
expiry=datetime.datetime.utcnow() + timedelta(hours=1)
)
url_with_sas = f"{url}?{sas_token}"
return redirect(url_with_sas)
我有一个 python 代码,可以从 azure blob 存储中下载 url 图像文件。如果我们在浏览器中复制粘贴此 url,它只会下载文件。我必须创建一个下载 api ,调用它时只会下载文件。以下是我目前拥有的代码:
from urllib.request import urlopen
from flask import Flask, jsonify
import uuid
from flask_cors import CORS
from datetime import datetime
from azure.storage.blob import generate_blob_sas, AccountSasPermissions
import datetime
from datetime import timedelta
app = Flask(__name__)
CORS(app)
uid_secret_key = str(uuid.uuid4())
app.secret_key = uid_secret_key
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app
@app.route('/download')
def download():
account_name = "deeusblobstorage"
container_name = "rperodct"
blob_name = "face_1_7285.jpg"
account_key = "hMlIRXCjAomfDRfied8Y5FPwTVnWEkEDuVsw//CQtkiOdcFD/y5wUcxH9Ou7Ni+DtDQFwe23YbZ3Qia9gw=="
url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"
sas_token = generate_blob_sas(
account_name=account_name,
account_key=account_key,
container_name=container_name,
blob_name=blob_name,
permission=AccountSasPermissions(read=True),
expiry=datetime.datetime.utcnow() + timedelta(hours=1)
)
url_with_sas = f"{url}?{sas_token}"
print(url_with_sas)
urlopen(url_with_sas)
@app.route('/')
def hello_world():
return jsonify({'Status': 'ok', 'date': '05.25.21'}), 200
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)
/download
api 包含从变量名 url_with_sas
中的 azure 容器下载 url blob 的代码。现在我想以这样的方式制作这个 API,如果我们点击它,它应该自动从 url 下载文件。所以为此我想到了添加 urlopen(url_with_sas)
这将打开 url。但似乎它不起作用。任何人都可以为此提出一些建议。谢谢
如评论中所述,解决方案是通过 return redirect(url_with_sas)
所以整个函数看起来像这样:
@app.route('/download')
def download():
account_name = "deeusblobstorage"
container_name = "rperodct"
blob_name = "face_1_7285.jpg"
account_key = "hMlIRXCjAomfDRfied8Y5FPwTVnWEkEDuVsw//CQtkiOdcFD/y5wUcxH9Ou7Ni+DtDQFwe23YbZ3Qia9gw=="
url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"
sas_token = generate_blob_sas(
account_name=account_name,
account_key=account_key,
container_name=container_name,
blob_name=blob_name,
permission=AccountSasPermissions(read=True),
expiry=datetime.datetime.utcnow() + timedelta(hours=1)
)
url_with_sas = f"{url}?{sas_token}"
return redirect(url_with_sas)