Created
November 12, 2017 23:30
-
-
Save Sniedes722/8dbb789e16b128e22b95d624a24577be to your computer and use it in GitHub Desktop.
Upload A File to an S3 Bucket with Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import boto3 | |
from flask import Flask, render_template, request, url_for, redirect | |
app = Flask(__name__) | |
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID'] | |
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY'] | |
S3_BUCKET = os.environ['S3_BUCKET'] | |
s3 = boto3.resource('s3', aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'], | |
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'], | |
region_name='us-west-2') | |
@app.route("/", methods=['GET','POST']) | |
def index(): | |
return render_template('index.html') | |
@app.route("/upload", methods=['POST']) | |
def upload(): | |
if request.files['file'] is not None: | |
file = request.files['file'] | |
s3.Bucket(S3_BUCKET).upload_fileobj(file, file.filename) | |
return redirect(url_for('index')) | |
else: | |
return redirect(url_for('index')) | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0", port=8080, debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Flask Upload to S3</title> | |
</head> | |
<body> | |
<h1>Flask Upload to S3</h1> | |
<form name="upload_file" action="/upload" method="POST" enctype="multipart/form-data"> | |
<input type="file" name="file"> | |
<input type="submit" value="Upload"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment