Created
January 23, 2018 04:17
-
-
Save caevyn/2aacdc7fa95193d1f7625924ae35d8dc 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
import boto3 | |
import json | |
import csv | |
import io | |
def lambda_handler(event, context): | |
s3 = boto3.resource('s3') | |
content_object = s3.Object('src-bucket', 'src-file.json') | |
file_content = content_object.get()['Body'].read().decode('utf-8') | |
lines = file_content.splitlines() | |
items = {} | |
for line in lines: | |
json_line = json.loads(line) | |
customer = json_line["Customer"] | |
items[customer["Email"]] = customer | |
toCSV = list(items.values()) | |
keys = toCSV[0].keys() | |
output = io.StringIO() | |
dict_writer = csv.DictWriter(output, keys) | |
dict_writer.writeheader() | |
dict_writer.writerows(toCSV) | |
s3.Object('my-items', 'items.csv').put(Body=output.getvalue()) | |
# Generate the URL to get 'key-name' from 'bucket-name' | |
url = boto3.client('s3').generate_presigned_url( | |
ClientMethod='get_object', | |
ExpiresIn= 3600, | |
Params={ | |
'Bucket': 'my-items', | |
'Key': 'items.csv' | |
} | |
) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment