-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
48 lines (39 loc) · 1.35 KB
/
Copy pathhandler.py
File metadata and controls
48 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random
import boto3
import os
import io
import base64
s3_client = boto3.client('s3')
def random_image(bucket_name):
response = s3_client.list_objects_v2(Bucket=bucket_name)
# print(response)
if 'Contents' in response:
objects = response['Contents']
# print(objects)
random_object = random.choice(objects)
print('random_object',random_object)
object_key = random_object['Key']
print('object_key', object_key)
response = s3_client.get_object(Bucket=bucket_name, Key=object_key)
image_data = response['Body'].read()
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-download-file.html
return image_data
# Base64 trick
# https://docs.aws.amazon.com/apigateway/latest/developerguide/lambda-proxy-binary-media.html
def lambda_handler(event, context):
bucket_name = os.environ.get('S3_BUCKET')
if bucket_name:
image_data = random_image(bucket_name)
if image_data:
return {
'isBase64Encoded': True,
'statusCode': 200,
'body': base64.b64encode(image_data).decode('utf-8'),
'headers': {
'Content-Type': 'image/jpeg'
}
}
return {
'statusCode': 400,
'body': 'Invalid request'
}