-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
60 lines (47 loc) · 1.77 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, flash, render_template, url_for, request, redirect
from werkzeug.utils import secure_filename
import boto3
import os
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
AWS_BUCKET = 'coders-toolkit-aws-demo' # Change this to your Bucket name
app = Flask(__name__)
app.secret_key = "some-unique-secret-key"
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def home():
return render_template('home.html')
@app.route("/s3-upload", methods=["GET", "POST"])
def upload_file():
if request.method == "POST":
# Let's use Amazon S3
s3 = boto3.resource('s3')
# Upload files to Amazon S3
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
s3.Bucket(AWS_BUCKET).put_object(Key=filename, Body=file)
flash('File Successfully uploaded')
return redirect(url_for('files'))
return render_template('upload.html')
@app.route("/s3-files", methods=["GET"])
def files():
# Let's use Amazon S3
s3 = boto3.resource('s3')
# Print out bucket names
# for bucket in s3.buckets.all():
# print(bucket.name)
# Get files from Bucket
contents = []
for item in s3.Bucket(AWS_BUCKET).objects.all():
contents.append(item)
app.logger.info(contents)
return render_template('list.html', files=contents)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=8080)