Skip to content

Instantly share code, notes, and snippets.

@ravishtiwari
Last active March 25, 2019 12:44
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 ravishtiwari/cb441dabe0944b3056ea14cea73144ae to your computer and use it in GitHub Desktop.
Save ravishtiwari/cb441dabe0944b3056ea14cea73144ae to your computer and use it in GitHub Desktop.
Python Upload File to S3 Bucket
def upload_to_s3(event, context):
body=str(event['body'])
#"""Assuming Following Body
#'------WebKitFormBoundarylaaNaialEQTotwSB\r\nContent-Disposition: form-data; name="file"; filename="test---.html"\r\nContent-Type: text/html\r\n\r\n<html>\n</html>\r\n------WebKitFormBoundarylaaNaialEQTotwSB--\r\n'
#"""
file_name = body.split(';')[2].split("\r\n")[0].split("=")[1].strip("\"")
c_type, c_data = parse_header(event['headers']['content-type'])
c_data['boundary'] = bytes(c_data['boundary'], "utf-8")
body_file = BytesIO(bytes(event['body'], "utf-8"))
form_data = parse_multipart(body_file, c_data)
s3 = boto3.resource('s3')
object = s3.Object(u'sl-html-demo',file_name)
object.put(Body=form_data['file'][0])
response = str(get_page(str(event), 'upload_response', '')).format("<b>File Uploaded : </b>" + file_name)
return return_html(response, str(event))
@ravishtiwari
Copy link
Author

image

@ravishtiwari
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment