Skip to content

Commit c900886

Browse files
cursoragentthomas.howe
andcommitted
Refactor: Simplify S3 key generation
Co-authored-by: thomas.howe <[email protected]>
1 parent 3dcbf18 commit c900886

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

server/storage/s3/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from lib.logging_utils import init_logger
44
from server.lib.vcon_redis import VconRedis
55
import boto3
6-
from datetime import datetime
76

87
logger = init_logger(__name__)
98

@@ -36,6 +35,14 @@ def _create_s3_client(opts: dict):
3635
return boto3.client("s3", **client_kwargs)
3736

3837

38+
def _build_s3_key(vcon_uuid: str, s3_path: Optional[str] = None) -> str:
39+
"""Build the S3 object key for a vCon."""
40+
key = f"{vcon_uuid}.vcon"
41+
if not s3_path:
42+
return key
43+
return f"{s3_path.rstrip('/')}/{key}"
44+
45+
3946
def save(
4047
vcon_uuid,
4148
opts=default_options,
@@ -46,13 +53,7 @@ def save(
4653
vcon = vcon_redis.get_vcon(vcon_uuid)
4754
s3 = _create_s3_client(opts)
4855

49-
s3_path = opts.get("s3_path")
50-
created_at = datetime.fromisoformat(vcon.created_at)
51-
timestamp = created_at.strftime("%Y/%m/%d")
52-
key = vcon_uuid + ".vcon"
53-
destination_directory = f"{timestamp}/{key}"
54-
if s3_path:
55-
destination_directory = s3_path + "/" + destination_directory
56+
destination_directory = _build_s3_key(vcon_uuid, opts.get("s3_path"))
5657
s3.put_object(
5758
Bucket=opts["aws_bucket"], Key=destination_directory, Body=vcon.dumps()
5859
)
@@ -67,10 +68,9 @@ def get(vcon_uuid: str, opts=default_options) -> Optional[dict]:
6768
"""Get a vCon from S3 by UUID."""
6869
try:
6970
s3 = _create_s3_client(opts)
70-
71-
s3_path = opts.get("s3_path", "")
72-
key = f"{s3_path}/{vcon_uuid}.vcon" if s3_path else f"{vcon_uuid}.vcon"
73-
71+
72+
key = _build_s3_key(vcon_uuid, opts.get("s3_path"))
73+
7474
response = s3.get_object(Bucket=opts["aws_bucket"], Key=key)
7575
return json.loads(response['Body'].read().decode('utf-8'))
7676

0 commit comments

Comments
 (0)