Skip to content

Commit

Permalink
adjust s3 object key
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
jschlyter committed Nov 3, 2023
1 parent b85dd40 commit f55cd51
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions aggrec/aggregates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from datetime import datetime
from enum import Enum
from functools import lru_cache
from typing import Annotated, Dict
Expand Down Expand Up @@ -134,6 +135,21 @@ def get_new_aggregate_event_message(
}


def get_s3_object_key(metadata: AggregateMetadata) -> str:
"""Get S3 object key from metadata"""
dt = metadata.id.generation_time
return "/".join(
[
f"type={metadata.aggregate_type.name.lower()}",
f"year={dt.year}",
f"month={dt.month}",
f"day={dt.day}",
f"creator={metadata.creator}",
f"id={metadata.id}",
]
)


@router.post("/api/v1/aggregate/{aggregate_type}")
async def create_aggregate(
aggregate_type: AggregateType,
Expand All @@ -160,30 +176,34 @@ async def create_aggregate(
location = f"/api/v1/aggregates/{aggregate_id}"

s3_bucket = settings.s3_bucket
s3_object_key = f"type={aggregate_type}/creator={creator}/{aggregate_id}"

if settings.s3_bucket_create:
try:
await s3_client.create_bucket(Bucket=s3_bucket)
except Exception:
pass

content = await request.body()
content_length = len(content)

await s3_client.put_object(Bucket=s3_bucket, Key=s3_object_key, Body=content)

metadata = AggregateMetadata(
id=aggregate_id,
aggregate_type=aggregate_type,
creator=creator,
http_headers=get_http_headers(request),
content_type=content_type,
content_length=content_length,
s3_bucket=s3_bucket,
s3_object_key=s3_object_key,
)

content = await request.body()

metadata.content_length = len(content)
metadata.s3_object_key = get_s3_object_key(metadata)

if settings.s3_bucket_create:
try:
await s3_client.create_bucket(Bucket=s3_bucket)
except Exception:
pass

await s3_client.put_object(
Bucket=s3_bucket, Key=metadata.s3_object_key, Body=content
)
logger.info("Object created: %s", metadata.s3_object_key)

metadata.save()
logger.info("Metadata saved: %s", metadata.id)

await mqtt_client.publish(
settings.mqtt_topic,
Expand Down

0 comments on commit f55cd51

Please sign in to comment.