Created
June 25, 2016 17:41
-
-
Save jesuslerma/0dde7ab9063943fecb6a1596479de73e to your computer and use it in GitHub Desktop.
This file contains 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 cherrypy | |
import boto | |
class Uploader(object): | |
@cherrypy.expose | |
@cherrypy.tools.json_out() | |
def index(self, myFile=None): | |
try: | |
FileValidator(myFile) | |
except ValueError as err: | |
return {"error": str(err)} | |
try: | |
return { "success": self.push_to_s3(myFile) } | |
except ValueError as err: | |
return {"error": "error uploading file"} | |
def push_to_s3(self, file): | |
s3 = boto.connect_s3() | |
bucket = s3.get_bucket('[bucket-name]') | |
key = bucket.new_key(file.filename) | |
key.set_contents_from_file(file.file) | |
#return path to get key of this shiit | |
class FileValidator(object): | |
def __init__(self, file_param): | |
if file_param is None: | |
raise ValueError('empty params') | |
if type(file_param) is not cherrypy._cpreqbody.Part: | |
raise ValueError('this is not a file param') | |
if str(file_param.content_type) != "text/xml": | |
raise ValueError('invalid value format') | |
if __name__ == '__main__': | |
cherrypy.quickstart(Uploader(), '/') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment