Created
April 13, 2016 22:59
-
-
Save westover/c56a314a6996eebd8010319df52ad820 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
from __future__ import print_function | |
import json | |
import urllib | |
import boto3 | |
import csv | |
import StringIO | |
print('Loading function') | |
s3 = boto3.client('s3') | |
transform_bucket = 'jamesfoo456' | |
def lambda_handler(event, context): | |
print("Received event: " + json.dumps(event, indent=2)) | |
# Get the object from the event and show its content type | |
bucket = event['Records'][0]['s3']['bucket']['name'] | |
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') | |
try: | |
response = s3.get_object(Bucket=bucket, Key=key) | |
body = response['Body'].read() | |
file_obj = StringIO.StringIO(body) | |
records = json.dumps([row for row in csv.DictReader(file_obj)]) | |
boto3.resource('s3').Object(transform_bucket, key.replace('.txt', '.json')).put(Body=records) | |
return 'OK' | |
except Exception as e: | |
print(e) | |
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment