0

To download files from S3 with Flask, I am using the following code:

@app.route('/downloads3', methods=['GET','POST'])
def download_s3():
    if not google.authorized:
        return redirect(url_for("google.login"))
    resp = google.get("/oauth2/v2/userinfo")
    tekst = resp.text
    user_data = json.loads(tekst)
    user_id = user_data['id']
    s3 = boto3.resource('s3')
    fileid = request.form.get('file_id')
    try: 
        s3.Bucket(user_id).download_file(str(fileid),current_app.root_path(str(fileid)))
        return 'gedownload'
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise
        return redirect('/s3')

The file is downloaded to the server but I want the file to be downloaded to the client instead.

Solaiman
  • 298
  • 2
  • 7
  • 22
  • 1
    This code doesn't do what you want at all. It downloads it to the server, not to the client. While you're in development those are the same machine, but once you deploy to production it won't download it to the user's machine at all. Instead you need to stream the file to the user and return that stream in the response. – Daniel Roseman Sep 13 '18 at 15:08
  • @DanielRoseman you are right, I have edited my question. Do you have any idea how I can make it work with Flask to stream the file to the client? I have used the following Boto3 documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-download-file.html – Solaiman Sep 13 '18 at 15:13

0 Answers0