我的flask应用程序总是调用第一个函数(从S3下载文件)



我创建了一个简单的flask应用程序,其中包含4个下载函数,用于从s3下载文件。我计划通过点击不同的内容下载不同的文件

from flask import Flask, Response, render_template
from boto3 import client
application = Flask(__name__)
bucket_name = 'bucket_name'
def get_client():
return client(
's3',
'us-east-1',
aws_access_key_id='myid',
aws_secret_access_key='mykey'
)

@application.route('/')
def home():
return render_template('Download.html')
@application.route('/download', methods=['GET'])
def Download_coin_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/token.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=token.zip"}
)

@application.route('/download', methods=['GET'])
def Download_transaction_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/transaction.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=transaction.zip"}
)
@application.route('/download', methods=['GET'])
def Download_index_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/index.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=index.zip"}
)
@application.route('/download', methods=['GET'])
def Download_commodity_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/commodity.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=commodity.zip"}
)

if __name__ == "__main__":
application.run(debug=True)

"download.html"文件:

<h2>Download cryptocurrency datasets</h2>
<p>
<a>NOTICE: Downloading a huge size set may take a little while:)</a>
</p>
<ul>
<p>
<li><a href="{{url_for('application.Download_coin_file')}}">Download_token_data(1100+ tokens price data)</a>
</p>
<p>
<li><a href="{{url_for('application.Download_transaction_file')}}">Download_transaction_data(ETH transaction data)</a>
</p>
<p>
<li><a href="{{url_for('application.Download_index_file')}}">Download_index_data (1300+ U.S. staock index data)</a>
</p>
<p>
<li><a href="{{url_for('application.Download_commodity_file')}}">Download_commodity_data (66 commodities price data)</a>
</p>
</ul>

但是,无论我点击哪个链接,应用程序都将调用第一个函数(download_coin_file)并开始下载"token.zip"文件。如何解决这个问题?

每个路由的URL是相同的,所以从技术上讲,它只会调用从URL/download开始创建的第一个函数。尝试用不同的url更改它。希望能有所帮助。

所有函数的端点都是相同的,因此我假设它是第一个。这意味着您在HTML部分中指向相同的端点。您应该更改端点,以获得所需的功能。

@application.route('/download-coin-file', methods=['GET'])
def Download_coin_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/token.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=token.zip"}
)

@application.route('/download-transaction-file', methods=['GET'])
def Download_transaction_file():
s3 = get_client()
target = s3.get_object(Bucket=bucket_name, Key='data/zip_data/transaction.zip')
return Response(
target['Body'].read(),
mimetype='zip',
headers={"Content-Disposition": "attachment;filename=transaction.zip"}
)

最新更新