Skip to content

Instantly share code, notes, and snippets.

@minusworld
Created October 6, 2021 16:58
Show Gist options
  • Select an option

  • Save minusworld/1115313f7c294bee757cf96a6dc4f7b9 to your computer and use it in GitHub Desktop.

Select an option

Save minusworld/1115313f7c294bee757cf96a6dc4f7b9 to your computer and use it in GitHub Desktop.
import json
import boto3
import os
import io
BUCKET="YOUR_BUCKET_HERE"
s3 = boto3.client("s3")
def lambda_handler(event, context):
path = event['queryStringParameters'].get("path", "")
item = event['queryStringParameters'].get("item", "latest")
r = s3.list_objects_v2(
Bucket=BUCKET,
Prefix=path,
)
items = r['Contents']
obj_key = f"{path}{'/' if path else ''}{item}"
print(f"Getting '{obj_key}' from '{BUCKET}'")
if item == "latest":
match = list(sorted(items, key=lambda k: k['LastModified']))[-1]
else:
match = filter(lambda k: obj_key == k['Key'])
buffer = io.BytesIO()
s3.download_fileobj(BUCKET, match['Key'], buffer)
buffer.seek(0)
data = buffer.read().decode('utf-8')
print(f"Got {len(data)} bytes: {data[:24]}...")
return json.loads(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment