Skip to content

Commit 85d7c2c

Browse files
✨ Add make_public parameter to file storage upload_file
1 parent 5a64350 commit 85d7c2c

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

electro/toolkit/files_storage/storage_services/_base_storage_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class BaseStorageService(ABC):
88
"""Base class for storage services."""
99

1010
@abstractmethod
11-
async def upload_file(self, file_io: BytesIO, content_type: str) -> str:
11+
async def upload_file(self, file_io: BytesIO, content_type: str, *, make_public: bool = False) -> str:
1212
"""Uploads an file to the storage and returns the object key.
1313
1414
:param file_io: BytesIO object of the file to upload
15+
:param content_type: MIME type of the file
16+
:param make_public: If True, make the file publicly accessible (S3: sets ACL to public-read)
1517
:return: object key of the uploaded file
1618
1719
"""

electro/toolkit/files_storage/storage_services/azure_blob_storage_service.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ async def _ensure_container_exists(self):
4444
except ResourceNotFoundError:
4545
await container_client.create_container()
4646

47-
async def upload_file(self, file_io: BytesIO, content_type: str) -> str:
48-
"""Upload an file to the Azure Blob Storage."""
47+
async def upload_file(self, file_io: BytesIO, content_type: str, *, make_public: bool = False) -> str:
48+
"""Upload an file to the Azure Blob Storage.
49+
50+
Note: make_public is accepted for API consistency but Azure blob public access
51+
is controlled at the container level. Use SAS tokens via get_file_url() for access.
52+
"""
53+
_ = make_public # Azure public access is container-level, not per-blob
4954
blob_name = f"file_{os.urandom(8).hex()}.png"
5055
async with await self.blob_service_client as client:
5156
await self._ensure_container_exists()

electro/toolkit/files_storage/storage_services/s3_service.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,21 @@ async def _download_file(self, object_key: str, destination: str | BytesIO | Non
6363

6464
return destination
6565

66-
async def upload_file(self, file_io: BytesIO, content_type: str) -> str:
66+
async def upload_file(self, file_io: BytesIO, content_type: str, *, make_public: bool = False) -> str:
6767
"""Uploads an file to the S3 bucket and returns the object key.
6868
6969
:param file_io: BytesIO object of the file to upload
70+
:param content_type: MIME type of the file
71+
:param make_public: If True, set ACL to public-read for public access
7072
:return: object key of the uploaded file
7173
7274
"""
7375
object_key = str(uuid4())
76+
extra_args = {"ContentType": content_type}
77+
if make_public:
78+
extra_args["ACL"] = "public-read"
7479
try:
75-
# TODO: [2024-10-05 by Mykola] IT'S NOT ALWAYS JPEG
76-
await self._upload_file(file_io, object_key, extra_args={"ContentType": content_type})
80+
await self._upload_file(file_io, object_key, extra_args=extra_args)
7781
logger.info(f"File uploaded successfully: {object_key}")
7882
return object_key
7983
except Exception as e:

electro/toolkit/files_storage/universal_file_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def __init__(self, storage_service: BaseStorageService):
1717
"""Initialize the UniversalFileStorage class."""
1818
self.storage_service = storage_service
1919

20-
async def upload_file(self, file_io: BytesIO, content_type: str) -> str:
20+
async def upload_file(self, file_io: BytesIO, content_type: str, *, make_public: bool = False) -> str:
2121
"""Upload an file to the storage service."""
22-
return await self.storage_service.upload_file(file_io, content_type)
22+
return await self.storage_service.upload_file(file_io, content_type, make_public=make_public)
2323

2424
async def download_file(self, object_key: str) -> BytesIO:
2525
"""Download an file from the storage service."""

0 commit comments

Comments
 (0)