Skip to content

Instantly share code, notes, and snippets.

@jesuslerma
Created June 25, 2016 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesuslerma/0dde7ab9063943fecb6a1596479de73e to your computer and use it in GitHub Desktop.
Save jesuslerma/0dde7ab9063943fecb6a1596479de73e to your computer and use it in GitHub Desktop.
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