-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(file_service/upload-multiple-file): split the lambda_functio…
…n into several files based on aws service and functionality (SCRUM-71)
- Loading branch information
Showing
5 changed files
with
236 additions
and
92 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import logging | ||
import os | ||
import re | ||
|
||
import time_util | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import logging | ||
import os | ||
from typing import Any, Dict | ||
|
||
import boto3 | ||
import time_util | ||
|
||
# Set up logging | ||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
dynamodb = boto3.client("dynamodb") | ||
DYNAMODB_TABLE = os.environ["DYNAMODB_TABLE"] | ||
|
||
|
||
def save_to_dynamodb( | ||
file_id: str, | ||
s3_object_key: str, | ||
file_url: str, | ||
file_name: str, | ||
file_size: int, | ||
uploader_id: str, | ||
) -> Dict[str, Any]: | ||
formatted_now = time_util.get_current_utc_time() | ||
|
||
dynamodb.put_item( | ||
TableName=DYNAMODB_TABLE, | ||
Item={ | ||
"file_id": {"S": file_id}, | ||
"s3_object_key": {"S": s3_object_key}, | ||
"created_at": {"S": formatted_now}, | ||
"updated_at": {"S": formatted_now}, | ||
"file_url": {"S": file_url}, | ||
"file_name": {"S": file_name}, | ||
"file_extension": {"S": file_name.split(".")[-1]}, | ||
"file_size": {"N": str(file_size)}, | ||
"uploader_id": { | ||
"S": uploader_id | ||
}, # Replace with actual uploader ID if available | ||
}, | ||
) | ||
|
||
return { | ||
"file_id": file_id, | ||
"s3_object_key": s3_object_key, | ||
"created_at": formatted_now, | ||
"updated_at": formatted_now, | ||
"file_url": file_url, | ||
"file_name": file_name, | ||
"file_extension": file_name.split(".")[-1], | ||
"file_size": file_size, | ||
"uploader_id": "dummy_uploader_id", # Replace with actual uploader ID if available | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import datetime | ||
|
||
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" | ||
|
||
|
||
def get_current_utc_time() -> str: | ||
""" | ||
Get the current UTC time and format it as ISO 8601. | ||
:return: Current UTC time in ISO 8601 format. | ||
""" | ||
return datetime.datetime.now(datetime.timezone.utc).strftime(TIME_FORMAT) | ||
|
||
|
||
def format_time_to_iso8601(dt: datetime.datetime) -> str: | ||
""" | ||
Format a datetime object as ISO 8601. | ||
:param dt: Datetime object. | ||
:return: Formatted time as ISO 8601 string. | ||
""" | ||
if dt.tzinfo is None: | ||
dt = dt.replace(tzinfo=datetime.timezone.utc) | ||
return dt.strftime(TIME_FORMAT) | ||
|
||
|
||
def parse_iso8601_to_datetime(iso8601_str: str) -> datetime.datetime: | ||
""" | ||
Parse an ISO 8601 string to a datetime object. | ||
:param iso8601_str: ISO 8601 formatted string. | ||
:return: Datetime object. | ||
""" | ||
return datetime.datetime.strptime(iso8601_str, TIME_FORMAT).replace( | ||
tzinfo=datetime.timezone.utc | ||
) | ||
|
||
|
||
def add_hours_to_time(iso8601_str: str, hours: int) -> str: | ||
""" | ||
Add a specified number of hours to an ISO 8601 time string. | ||
:param iso8601_str: ISO 8601 formatted string. | ||
:param hours: Number of hours to add. | ||
:return: New ISO 8601 formatted time string. | ||
""" | ||
dt = parse_iso8601_to_datetime(iso8601_str) | ||
new_dt = dt + datetime.timedelta(hours=hours) | ||
return format_time_to_iso8601(new_dt) | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
current_time = get_current_utc_time() | ||
print("Current UTC Time:", current_time) | ||
|
||
formatted_time = format_time_to_iso8601( | ||
datetime.datetime.now(datetime.timezone.utc) | ||
) | ||
print("Formatted Time:", formatted_time) | ||
|
||
parsed_time = parse_iso8601_to_datetime("2024-07-11T12:00:00Z") | ||
print("Parsed Time:", parsed_time) | ||
|
||
new_time = add_hours_to_time("2024-07-11T12:00:00Z", 3) | ||
print("New Time:", new_time) |