TECHIES WORLD

For Techs.... Techniques.... Technologies....

AWSBashLinux

How to configure S3 object downloader using Python

Amazon Simple Storage Service (S3) is an offering by Amazon Web Services (AWS) that allows users to store data in the form of objects. This article explains the steps to configure a client application using Python to download the S3 objects easily. This will be help full for enabling download links for objects in private S3 buckets.

Step1: Install awscli.

#pip install awscli

Step2: Configure awscli.

#aws configure

Here we need to provide the aws access key and secret key as standard input. Please note that the user belongs to this key must have permissions over S3 bucket.

Step3: Create the virtual environment in Python.

#virtualenv s3downloader -p python3

Step4: Change the path to the newly created folder.

#cd s3downloader

Step5: Activate the virtual environment.

#source bin/activate

Step6: Install Flask.

#pip install flask

Step7: Install boto3.

#pip install boto3

Step8: Create one file with name "s3_demo.py" and save the download function in that file.

import boto3

def download_file(file_name, bucket):
    """
    Function to download a given file from an S3 bucket
    """
    s3 = boto3.resource('s3')
    output = f"downloads/{file_name}"
    s3.Bucket(bucket).download_file(file_name, output)

    return output

Step9: Create a file with name app.py and save the following content.

import os
from flask import Flask, render_template, request, redirect, send_file
from s3_demo import download_file

app = Flask(__name__)
BUCKET = "NAME"

@app.route("/download/<filename>", methods=['GET'])
def download(filename):
    if request.method == 'GET':
        output = download_file(filename, BUCKET)

        return send_file(output, as_attachment=True)

if __name__ == '__main__':
    app.run(debug=True)

Where NAME need to be replaced with the name of the S3 bucket.

Step10: Create the downloads folder.

#mkdir downloads

Step11: Start the application.

#python app.py

Application will start running on the port 5000 and the base url will be http://localhost:5000/storage. We need to append the required object name to the url for download.

Syntax: http://localhost:5000/storage/object-name

That's all…

Leave a Reply