def handler(event, context) -> Dict: """ Main function """ bg_id = None try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(event) print(e) if bg_id is not None: print(f"Bookmark group id: {bg_id}") with _get_db_connection() as conn: result = _delete_bookmark_group(bg_id, conn) conn.commit() print(f"deleted bookmarkgroup: {result}") # Delete bookmark group name in elasticsearch _delete_index(result) return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, }
def handler(event, context) -> Dict: """ Main function """ banner_id = None response_body = {} try: banner_id = int(event["pathParameters"]["banner_id"]) except Exception as e: print("Cannot get banner id") print(event) print(e) if banner_id is not None: print(f"Banner id: {banner_id}") with _get_db_connection() as conn: result = _get_bg_ids(banner_id, conn) conn.commit() print("Got bookmark group id: ") print(result) response_body = { "ids": result } return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps(response_body, ensure_ascii=False) }
def handler(event, context) -> Dict: """ Main function """ bg_id = None try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(event) print(e) try: tag_name = urllib.parse.unquote(event["pathParameters"]["name"]) except Exception as e: print("Cannot get tag name") print(event) print(e) if len(tag_name.strip()) == 0: print("Tag is empty string") if bg_id is not None and len(tag_name.strip()) != 0: print(f"Tag name: {tag_name}") print(f"Bookmarkgroup id: {bg_id}") with _get_db_connection() as conn: result = _add_tag_db(bg_id, tag_name, conn) conn.commit() print("Tag added") print(result) if len(result) > 0: _update_tag_es(bg_id, result) return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps({ 'id': bg_id, 'tags': [tag["name"] for tag in result] }, ensure_ascii=False) }
def handler(event, context): """ Main function """ bg_id = None return_body = {"url": None} try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmar_group_id") print(event) print(e) raise ValueError with _get_db_connection() as conn: results = _get_banner_urls(bg_id, conn) conn.commit() print(f"Total banners: {len(results)}") if len(results) > 0: out_s3_key = f'tmp/{str(uuid.uuid4())}/banners.zip' out_s3_uri = f's3://{IO_BUCKET}/{out_s3_key}' # out_s3_uriにの空のファイルを置いておく s3_bucket = S3Bucket(os.environ['AWS_REGION'], IO_BUCKET) s3_bucket.put_object(out_s3_key, '', 'application/zip') # ブラウザ側からout_s3_uriをGET可能な一時URLを発行する presigned_url = s3_bucket.generate_presigned_get_url(out_s3_key, expires_in=PRESIGNED_URL_EXPIRATION_PERIOD) return_body["url"] = presigned_url # CSV出力を実行するLambda関数を非同期に呼び出す # 結果はout_s3_uriに書き込まれる response = boto3.client('lambda').invoke( FunctionName=os.environ['BULK_IMAGE_FUNCTION_NAME'], InvocationType='Event', Payload=json.dumps({"banner_list": results, "out_s3_uri": out_s3_uri}, indent=2, ensure_ascii=False) ) return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps(return_body) }
def handler(event, context) -> Dict: """ Main function """ response_body = {} air_user_id = None try: air_user_id = event["requestContext"]["authorizer"]["claims"][ "custom:air_user_id"] except KeyError as e: print(e) print("Cannot get air_user_id") body_data = json.loads(event["body"]) bg_name = body_data["name"] if len(bg_name.strip()) == 0: print("Bookmark group name is empty") if air_user_id is not None and len(bg_name.strip()) != 0: air_user_id = int(air_user_id) print(f"air_user_id: {air_user_id}") print(f"Create bookmar group: {bg_name}") with _get_db_connection() as conn: result = _create_bookmark_group(bg_name, air_user_id, conn) result["air_user_id"] = air_user_id conn.commit() print(f"Bookmark group: {result}") # Add new bookmarkgroup to elasticsearch _indexing(result) response_body = {"id": result["id"]} print(f"response body: {response_body}") return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps(response_body, ensure_ascii=False) }
def handler(event, context) -> Dict: """ Main function """ try: bg_id = int(event["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(f"Event: {event}") print(e) print(f"Bookmarkgroup id: {bg_id}") with _get_db_connection() as conn: result = _get_banners_data(bg_id, conn) conn.commit() with io.StringIO() as f: writer = csv.DictWriter(f, CSV_COLUMNS) writer.writeheader() for row in result: try: writer.writerow(row) except Exception as e: print(f"Error in writing csw row: {e}") print("row") raise ValueError csv_data = f.getvalue() # 結果を指定されたS3に書き込む if 'out_s3_uri' in event: # out_s3_uri = 's3://{bucket}/{key}' parts = event['out_s3_uri'].split('/') bucket = parts[2] key = '/'.join(parts[3:]) s3_bucket = S3Bucket(os.environ['AWS_REGION'], bucket) s3_bucket.put_object(key, csv_data, 'text/csv') return { 'statusCode': 200, 'headers': { 'Content-Type': 'text/csv', 'Access-Control-Allow-Origin': '*' }, 'body': csv_data }
def handler(event, context) -> Dict: """ Main function """ response_body = {} bg_id = None try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(event) print(e) body_data = json.loads(event["body"]) bg_name = body_data["name"] if len(bg_name.strip()) == 0: print("Bookmark group name is empty") if bg_id is not None and len(bg_name.strip()) != 0: print(f"Bookmark group id: {bg_id}") print(f"Create bookmar group: {bg_name}") with _get_db_connection() as conn: result = _change_bookmark_group_name(bg_id, bg_name, conn) conn.commit() print(f"Result: {result}") # Update bookmark group name in elasticsearch _indexing(result) response_body = { "id": result["id"], "name": result["name"] } print(f"response body: {response_body}") return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps(response_body, ensure_ascii=False) }
def handler(event, context) -> Dict: """ Main function """ bg_id = None air_user_id = None try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(event) print(e) try: air_user_id = event["requestContext"]["authorizer"]["claims"]["custom:air_user_id"] except KeyError as e: print(e) print("Cannot get air_user_id") body_data = json.loads(event["body"]) banner_ids = body_data["ids"] if len(banner_ids) == 0: print("Banner array is empty") if bg_id is not None and len(banner_ids) != 0 and air_user_id is not None: print(f"Banner ids: {banner_ids}") print(f"Bookmarkgroup id: {bg_id}") with _get_db_connection() as conn: result = _add_banner_ids(bg_id, banner_ids, air_user_id, conn) conn.commit() print("Banners added") print(result) if "thumbnail_url" in result: _update_document(result) return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }
def handler(event, context) -> Dict: """ Main function """ response_body = {} air_user_id = None try: air_user_id = event["requestContext"]["authorizer"]["claims"]["custom:air_user_id"] except KeyError as e: print(e) print("Cannot get air_user_id") if air_user_id is not None: air_user_id = int(air_user_id) print(f"air_user_id: {air_user_id}") with _get_db_connection() as conn: result = _get_bookmark_group_recent(air_user_id, conn) if len(result) != 0: response_body = { "id": result[0]["id"], "name": result[0]["name"], "bookmarkCount": result[0]["bookmark_count"], "thumbnailUrl": result[0]["thumbnail_url"], "creatorAirUserID": result[0]["air_user_id"], "tags": result[0]["tags"] } print(f"response body: {response_body}") return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, 'body': json.dumps(response_body, ensure_ascii=False) }
def handler(event, context) -> Dict: """ Main function """ bg_id = None try: bg_id = int(event["pathParameters"]["bookmark_group_id"]) except Exception as e: print("Cannot get bookmark group id") print(event) print(e) body_data = json.loads(event["body"]) banner_ids = body_data["ids"] if len(banner_ids) == 0: print("Banner array is empty") if bg_id is not None and len(banner_ids) != 0: print(f"Banner ids: {banner_ids}") print(f"Bookmarkgroup id: {bg_id}") with _get_db_connection() as conn: banner_ids = _delete_banner_ids(bg_id, banner_ids, conn) conn.commit() print("Banners deleted") if len(banner_ids) > 0: _update_document({"id": bg_id, "banner_ids": banner_ids}) return { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }