Last active
March 25, 2019 12:44
-
-
Save ravishtiwari/cb441dabe0944b3056ea14cea73144ae to your computer and use it in GitHub Desktop.
Python Upload File to S3 Bucket
This file contains hidden or 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
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)) |
Author
ravishtiwari
commented
Mar 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment