#!/usr/bin/env python3
import json
import boto3
import time
import urllib.parse
import pprint
import tarfile
import os
def lambda_handler(event, context):
s3 = boto3.client(
's3',
region_name='us-east-1',
)
if (event['body'] is not None):
print("Body is not none!")
content = event['body']
postid = dict(urllib.parse.parse_qsl(content))['postid']
else:
print("Body Is None!")
content = pprint.pprint(event)
postid = "ERROR"
os.chdir("/tmp")
fname = postid + " " + str(time.time()) + ".txt"
with open(fname, "w") as f:
f.write(content)
bucket = "ms98-firstbucket"
commentsfile = "comments.tar"
s3.download_file(bucket, commentsfile, commentsfile)
with tarfile.open(commentsfile, "a") as archive:
archive.add(fname, arcname=fname)
s3.delete_object(Bucket = bucket, Key = commentsfile)
with open(commentsfile, "rb") as f:
s3.upload_fileobj(f, bucket, commentsfile)
http_res = {}
http_res['statusCode'] = 200
http_res['headers'] = {}
http_res['headers']['Content-Type'] = 'application/json'
http_res['body'] = "Comment submitted!"
return http_res