Last active
May 12, 2024 08:06
-
-
Save govind-kumarr/e838b4745bc96b0227854bbad9b8e1cc 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
# https://nr3wrniofl.execute-api.ap-south-1.amazonaws.com/avatars/test2 | |
def get_image(event, context): | |
image_name = event["pathParameters"]["image_name"] | |
key = f"avatars/{image_name}" | |
bucket_name = "govind-public-assets" | |
bucket = s3.Bucket(bucket_name) | |
obj = bucket.Object(key) | |
image_data = obj.get()["Body"].read() | |
response = { | |
"headers": {"Content-Type": "image/png"}, | |
"statusCode": 200, | |
"body": base64.b64encode(image_data), | |
"isBase64Encoded": True, | |
} | |
return response | |
# Example :- https://nr3wrniofl.execute-api.ap-south-1.amazonaws.com/avatars?image_name=test2 | |
def upload_image(event, context): | |
response: dict = {} | |
if not "queryStringParameters" in event: | |
response.update( | |
{ | |
"statusCode": 400, | |
"body": "Missing image name. Please include image name in your query params", | |
} | |
) | |
return response | |
query_str = event["queryStringParameters"] | |
if not "image_name" in query_str: | |
response.update( | |
{ | |
"statusCode": 400, | |
"body": "Missing image name. Please include image name in your query params", | |
} | |
) | |
return response | |
image_name = query_str["image_name"] | |
content_type: str = event["headers"]["content-type"] | |
if content_type and "image/" not in content_type: | |
return {"statusCode": 400, "body": "Invalid content type"} | |
image_type = content_type.split("/")[1] | |
image_data = base64.b64decode(event["body"]) | |
key = f"avatars/{image_name}.{image_type}" | |
print({"key": key}) | |
bucket_name = "govind-public-assets" | |
bucket = s3.Bucket(bucket_name) | |
obj = bucket.Object(key) | |
obj.put(Body=image_data) | |
response = {"statusCode": 200, "body": "Image uploaded successfully!"} | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment