Home > Net >  My flask application always call the first funciton (download file from S3)
My flask application always call the first funciton (download file from S3)

Time:05-08

I create a simple flask application with 4 download functions to download files from s3. I plan to download different files by clicking different content

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)

The "download.html" file:

<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>

However, no matter which links I click, the application will call the first function(download_coin_file) and start to download the "token.zip" file. How to fix the problem?

CodePudding user response:

the URL of each route is the same, so technically it will call only the first function that has been created from start with the URL /download. Try to change it with different URLs.Hope it helps.

CodePudding user response:

The endpoints for all the functions are the same, thus I am assuming it is going to the first one. Meaning that you are pointing to the same endpoint in the HTML part. You should change the endpoints, for the functionality you want.

@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"}
    )
  • Related