Skip to content

Instantly share code, notes, and snippets.

@shentonfreude
Created April 22, 2015 18:15
Show Gist options
  • Save shentonfreude/f960fe48f2c957dd2204 to your computer and use it in GitHub Desktop.
Save shentonfreude/f960fe48f2c957dd2204 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.view import view_defaults
import boto3
AWS_REGION = "us-east-1"
S3_BUCKET = "avail-assets-test"
SQS_URL = "https://sqs.us-east-1.amazonaws.com/355255540862/avail-incoming-images"
# The SQS_URL changes with each new CloudFormation creation :-(
@view_config(route_name="asset_upload_form", request_method="GET")
def asset_upload_form(request):
"""Present a simple upload asset form.
"""
return Response("""
<html>
<head><title>Upload Asset</title></head>
<body>
<form action="{}" method="POST"
accept-charset="utf-8" enctype="multipart/form-data">
<label for="upload_file">Upload asset file: </label>
<input id="upload_file" name="upload_file" type="file" value="">
<input id="autopublish" name="autopublish" type="checkbox">Autopublish
<input type="submit" value="Upload">
</form>
</body>
</html>
""".format(request.route_url("assets", id=None)))
@view_defaults(route_name="assets")
class AssetsView(object):
def __init__(self, request):
self.request = request
@view_config(request_method="GET")
def get(self, id=None):
"""TODO: list all assets in from S3
"""
return Response("This should show assets for id={}".format(id))
# TODO: view for /assets/{assetname}
@view_config(request_method="POST", accept="multipart/form-data",
request_param="upload_file", renderer="json")
def post(self):
"""Stream the file to an S3 object.
POST via command line like:
curl --form [email protected] \
--form autopublish=on http://127.0.0.1:8888/assets/
Can we sneak-and-peek and do any other processing here like
extracting EXIF?
"""
import pdb; pdb.set_trace()
upload = self.request.POST["upload_file"]
upload.file.seek(0)
bucket = boto3.resource("s3").Bucket(S3_BUCKET)
# put(): ContentType, ContentLength, Metadata
# returns {'ETag': ..., 'ResponseMetadata': {'HTTPStatusCode': 200, ...}}
bucket.Object(upload.filename).put(Body=upload.file) # returns dict
# TODO: grab 'autopublish'; form sets it to 'on' or doesn't send it at all if not set.
# TODO: check and handle upload failures
try:
session = boto3.session.Session(region_name=AWS_REGION)
sqs = session.resource("sqs")
queue = sqs.Queue(SQS_URL)
queue.send_message(MessageBody=upload.filename)
except:
return {'status': 'SQS FAIL', 'name': upload.filename, 'type': upload.type}
return {'status': 'ok', 'name': upload.filename, 'type': upload.type}
@view_config(request_method="POST", renderer='json')
def post_catchall(self):
self.request.response.status = 400
return {"status": "error",
"msg": ("Send multipart/form-data with field 'upload_file'" +
" and optional 'autopublish' of on/off; example:" +
" curl --form [email protected]" +
"--form autopoublish=on http://127.0.0.1/assets/")}
def _push_queue():
"""Once file uploaded, add it to the queue"""
def _pull_queue(request):
"""TBD"""
if __name__ == "__main__":
config = Configurator()
config.scan()
config.add_route("asset_upload_form", "/")
config.add_route("assets", "/assets/*id") # how do I spec optional `id`?
app = config.make_wsgi_app()
server = make_server("0.0.0.0", 8888, app)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment