-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask-s3-upload.py
130 lines (96 loc) · 3.18 KB
/
flask-s3-upload.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from flask import Flask, render_template, request, redirect
from werkzeug.utils import secure_filename
import os
import boto3
from cfenv import AppEnv
env=AppEnv()
print env.name
print env.port
s3bucket = env.get_service(name='s3bucket')
print s3bucket.credentials
app = Flask(__name__)
#app.config.from_object("config.config")
#from .helpers import *
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
@app.route("/")
def index():
# print "S3_BUCKET: " + app.config["S3_BUCKET"]
# print "S3_ACCESS_KEY: " + app.config["S3_ACCESS_KEY"]
# print "S3_SECRET_KEY: " + app.config["S3_SECRET_KEY"]
# print "S3_LOCATION: " + app.config["S3_LOCATION"]
# print s3
return render_template("index.html")
def upload_file_to_s3(file, bucket_name, acl="public-read"):
"""
Docs: http://boto3.readthedocs.io/en/latest/guide/s3.html
"""
try:
s3.upload_fileobj(
file,
bucket_name,
file.filename,
ExtraArgs={
"ACL": acl,
"ContentType": file.content_type
}
)
except Exception as e:
print("Something Bad Happened: ", e)
print file
print bucket_name
print acl
print file.content_type
return e
return "{}{}".format(S3_LOCATION, file.filename)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/", methods=["POST"])
def upload_file():
# A
if "user_file" not in request.files:
return "No user_file key in request.files"
# B
file = request.files["user_file"]
"""
These attributes are also available
file.filename # The actual name of the file
file.content_type
file.content_length
file.mimetype
"""
# C.
if file.filename == "":
return "Please select a file"
# D.
if file and allowed_file(file.filename):
file.filename = secure_filename(file.filename)
# output = upload_file_to_s3(file, app.config["S3_BUCKET"])
output = upload_file_to_s3(file, S3_BUCKET)
return '<html><a href="' + str(output) + '">'+ str(output) +'</a></html>'
else:
return redirect("/")
if __name__ == '__main__':
env = AppEnv()
print "env.name: "+ env.name
print "env.port: "+ str(env.port)
s3bucket = env.get_service(name='s3bucket')
creds = s3bucket.credentials
print "env.creds.bucket: "+creds['S3_BUCKET']
print "env.creds.ACCESS_KEY"+creds['S3_ACCESS_KEY']
print "env.creds.ACCESS_KEY"+creds['S3_SECRET_KEY']
S3_BUCKET=creds['S3_BUCKET']
S3_ACCESS_KEY=creds['S3_ACCESS_KEY']
S3_SECRET_KEY=creds['S3_SECRET_KEY']
S3_LOCATION = 'http://{}.s3.amazonaws.com/'.format(S3_BUCKET)
print "S3_BUCKET: " + S3_BUCKET
print "S3_ACCESS_KEY: " + S3_ACCESS_KEY
print "S3_SECRET_KEY: " + S3_SECRET_KEY
print "S3_LOCATION: " + S3_LOCATION
s3 = boto3.client(
's3',
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
)
port = int(os.getenv("PORT"))
app.run(host='0.0.0.0', port=port)