Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Lambda Cheat Sheet

09 May 2020 » aws

load & save from/to s3

import boto3
import json

s3 = boto3.resource('s3')
    
def load_s3(bucket, key):
    obj = s3.Object(bucket, key)
    body = obj.get()['Body'].read()
    return json.loads(body.decode())
    
def save_s3(data, bucket, key):
    obj = s3.Object(bucket, key)
    obj.put(Body=json.dumps(data).encode())


def lambda_handler(event, context):
    # TODO implement
    
    bucket = '<bucket name>'
    key = '<keyname>'
    body = load_s3(bucket, key)
    print(body)
    body["a"] = "b"
    save_s3(body, bucket, key)