Skip to content

Commit

Permalink
remove downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Aug 20, 2024
1 parent a2816a7 commit b9e29b3
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
13 changes: 2 additions & 11 deletions store/app/crud/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from xml.etree import ElementTree as ET

import trimesh
from aiobotocore.response import StreamingBody
from boto3.dynamodb.conditions import ComparisonCondition
from fastapi import UploadFile
from PIL import Image
Expand Down Expand Up @@ -65,7 +64,7 @@ async def _crop_image(self, image: Image.Image, size: tuple[int, int]) -> IO[byt
async def _upload_cropped_image(self, image: Image.Image, artifact: Artifact, size: ArtifactSize) -> None:
image_bytes = await self._crop_image(image, SizeMapping[size])
filename = get_artifact_name(artifact=artifact, size=size)
await self._upload_to_s3(image_bytes, artifact.name, filename, "image/png", private=False)
await self._upload_to_s3(image_bytes, artifact.name, filename, "image/png")

async def _upload_image(
self,
Expand Down Expand Up @@ -165,7 +164,7 @@ async def _upload_and_store(
description=description,
)
await asyncio.gather(
self._upload_to_s3(file, name, get_artifact_name(artifact=artifact), content_type, private=True),
self._upload_to_s3(file, name, get_artifact_name(artifact=artifact), content_type),
self._add_item(artifact),
)
return artifact
Expand Down Expand Up @@ -206,14 +205,6 @@ async def _remove_raw_artifact(self, artifact: Artifact) -> None:
self._delete_item(artifact),
)

async def stream_artifact(
self,
artifact: Artifact,
private: bool = True,
size: ArtifactSize = "large",
) -> StreamingBody:
return await self._download_from_s3(get_artifact_name(artifact=artifact, size=size), private=private)

async def remove_artifact(self, artifact: Artifact) -> None:
match artifact.artifact_type:
case "image":
Expand Down
10 changes: 4 additions & 6 deletions store/app/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async def _update_item(self, item_id: str, item_class: type[T], new_values: dict
AttributeUpdates={k: {"Value": v, "Action": "PUT"} for k, v in new_values.items() if k != "id"},
)

async def _upload_to_s3(self, data: IO[bytes], name: str, filename: str, content_type: str, private: bool) -> None:
async def _upload_to_s3(self, data: IO[bytes], name: str, filename: str, content_type: str) -> None:
"""Uploads some data to S3.
Args:
Expand All @@ -373,28 +373,26 @@ async def _upload_to_s3(self, data: IO[bytes], name: str, filename: str, content
filename: The resulting filename in S3 (should be unique).
content_type: The file content type, for CloudFront to provide
in the file header when the user retrieves it.
private: Whether the object is in the private S3 bucket.
"""
bucket = await self.s3.Bucket(settings.s3.bucket)
await bucket.put_object(
Key=f"{settings.s3.private_prefix if private else settings.s3.prefix}{filename}",
Key=f"{settings.s3.prefix}{filename}",
Body=data,
ContentType=content_type,
ContentDisposition=f'attachment; filename="{name}"',
)

async def _download_from_s3(self, filename: str, private: bool) -> StreamingBody:
async def _download_from_s3(self, filename: str) -> StreamingBody:
"""Downloads an object from S3.
Args:
filename: The filename of the object to download.
private: Whether the object is in the private S3 bucket.
Returns:
The object data.
"""
bucket = await self.s3.Bucket(settings.s3.bucket)
obj = await bucket.Object(f"{settings.s3.private_prefix if private else settings.s3.prefix}{filename}")
obj = await bucket.Object(f"{settings.s3.prefix}{filename}")
data = await obj.get()
return data["Body"]

Expand Down
1 change: 0 additions & 1 deletion store/settings/configs/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ crypto:
s3:
bucket: artifacts
prefix: media
private_prefix: private-media
dynamo:
table_name: kscale-store-local
site:
Expand Down
1 change: 0 additions & 1 deletion store/settings/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ArtifactSettings:
class S3Settings:
bucket: str = field(default=II("oc.env:S3_BUCKET"))
prefix: str = field(default=II("oc.env:S3_PREFIX"))
private_prefix: str = field(default=II("oc.env:S3_PRIVATE_PREFIX"))


@dataclass
Expand Down

0 comments on commit b9e29b3

Please sign in to comment.