Skip to content

Commit

Permalink
add S3 metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Nov 16, 2023
1 parent 9d454ef commit 23c369f
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion aggrec/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ def get_s3_object_key(metadata: AggregateMetadata) -> str:
return "/".join(fields_list)


def get_s3_object_metadata(metadata: AggregateMetadata) -> dict:
"""Get S3 object metadata from metadata"""
return {
"aggregate-id": str(metadata.id),
"aggregate-type": metadata.aggregate_type.value,
"created": metadata.id.generation_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"creator": str(metadata.creator),
**(
{
"interval-start": metadata.aggregate_interval_start.astimezone(
tz=timezone.utc
).strftime("%Y-%m-%dT%H:%M:%SZ"),
"interval-duration": str(metadata.aggregate_interval_duration),
}
if metadata.aggregate_interval_start
and metadata.aggregate_interval_duration
else {}
),
}


@router.post("/api/v1/aggregate/{aggregate_type}")
async def create_aggregate(
aggregate_type: AggregateType,
Expand Down Expand Up @@ -236,8 +257,14 @@ async def create_aggregate(
except Exception:
pass

s3_object_metadata = get_s3_object_metadata(metadata)
logger.debug("S3 object metadata: %s", s3_object_metadata)

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

Expand Down

0 comments on commit 23c369f

Please sign in to comment.