-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
68 lines (54 loc) · 1.66 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
61
62
63
64
65
66
67
68
import boto
import os
import sys
from boto.s3.key import Key
import web
urls = (
'/', 'Index',
'/upload', 'Upload',
'/push', 'Push'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
# this is apparently broken in latest boto HEAD because they
# like breaking things.
bucket_name = "zedshaw.uploadertest"
CONN = boto.connect_s3(anon=True)
BUCKET = CONN.get_bucket(bucket_name)
def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()
def upload_file(bucket, base, thename):
thefile = "%s/%s" % (base, thename)
k = bucket.get_key(thename)
if k and k.exists():
print "Already uploaded %s" % thefile
else:
print 'Uploading %s to Amazon S3 bucket %s' % \
(thefile, bucket_name)
k = Key(bucket)
k.key = thename
k.set_contents_from_filename(thefile,
cb=percent_cb, num_cb=10)
k.set_acl("public-read")
k.set_metadata("Content-Type", "application/ogg")
return thename
class Index(object):
def GET(self):
params = web.input(site=None)
return render.index(site=params.site)
class Upload(object):
def GET(self):
params = web.input(site = None)
uploads = os.listdir("uploads")
return render.upload(site=params.site, uploads=uploads)
class Push(object):
def GET(self):
params = web.input(site = None, target = None)
if not params.target:
return "Get it right."
else:
upload_file(BUCKET, "uploads", params.target)
return render.doingit(site = params.site, target = params.target)
if __name__ == "__main__":
app.run()