From 6b4edcf823646ecc6a90ab038afe9744f8ff8d66 Mon Sep 17 00:00:00 2001 From: Jason Park <93040528+JasonNotJson@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:34:44 +0900 Subject: [PATCH] feat: adding images for board view also (#334) * feat: adding images for board view also * fix: removing policies --- src/lambda/get-all-threads/index.py | 8 +++++++- src/lambda/get-all-threads/utils.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lambda/get-all-threads/index.py b/src/lambda/get-all-threads/index.py index e8c728c3d..f4f8ef67d 100644 --- a/src/lambda/get-all-threads/index.py +++ b/src/lambda/get-all-threads/index.py @@ -1,7 +1,7 @@ from boto3.dynamodb.conditions import Key import boto3 from datetime import datetime -from utils import JsonPayloadBuilder, table, resp_handler +from utils import JsonPayloadBuilder, table, resp_handler, s3_client, bucket, generate_url @resp_handler @@ -36,9 +36,15 @@ def get_all_threads(uid, index, num, school, tags, board_id): item['mod'] = True item['user_liked'] = uid in item.get('likes', []) item['total_likes'] = len(item.get('likes', [])) + if "obj_key" in item: + bucket_name = bucket + presigned_url = generate_url(bucket_name, item["obj_key"]) + if presigned_url: + item["url"] = presigned_url item.pop('uid', None) item.pop('likes', None) + item.pop('obj_key', None) body = JsonPayloadBuilder().add_status( True).add_data(paginated_items).add_message(end_index).compile() diff --git a/src/lambda/get-all-threads/utils.py b/src/lambda/get-all-threads/utils.py index 05f0218dc..28cd921a5 100644 --- a/src/lambda/get-all-threads/utils.py +++ b/src/lambda/get-all-threads/utils.py @@ -7,6 +7,9 @@ db = boto3.resource("dynamodb", region_name="ap-northeast-1") table = db.Table(os.getenv('TABLE_NAME')) +s3_client = boto3.client('s3') +bucket = os.getenv('BUCKET_NAME') + class DecimalEncoder(json.JSONEncoder): def default(self, obj): @@ -64,3 +67,16 @@ def handle(*args, **kwargs): return api_response(500, resp) return handle + + +def generate_url(bucket_name, object_key, expiration=3600): + try: + response = s3_client.generate_presigned_url('get_object', + Params={'Bucket': bucket_name, + 'Key': object_key}, + ExpiresIn=expiration) + except Exception as e: + logging.error(str(e)) + return None + + return response