Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 80454f9

Browse files
authored
feat: bucket level file controls and an update_bucket method (#103)
* feat: bucket level file controls and an update_bucket method * fix: import __future__ annotations python 3.8 doesn't allow subscripting types like list
1 parent 03c5b03 commit 80454f9

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

storage3/_async/bucket.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from httpx import HTTPError, Response
66

7-
from ..types import RequestMethod
7+
from ..types import CreateOrUpdateBucketOptions, RequestMethod
88
from ..utils import AsyncClient, StorageException
99
from .file_api import AsyncBucket
1010

@@ -52,7 +52,10 @@ async def get_bucket(self, id: str) -> AsyncBucket:
5252
return AsyncBucket(**json, _client=self._client)
5353

5454
async def create_bucket(
55-
self, id: str, name: Optional[str] = None, public: bool = False
55+
self,
56+
id: str,
57+
name: Optional[str] = None,
58+
options: Optional[CreateOrUpdateBucketOptions] = None,
5659
) -> dict[str, str]:
5760
"""Creates a new storage bucket.
5861
@@ -62,16 +65,37 @@ async def create_bucket(
6265
A unique identifier for the bucket you are creating.
6366
name
6467
A name for the bucket you are creating. If not passed, the id is used as the name as well.
65-
public
66-
Whether the bucket you are creating should be publicly accessible. Defaults to False.
68+
options
69+
Extra options to send while creating the bucket. Valid options are `public`, `file_size_limit` and
70+
`allowed_mime_types`.
6771
"""
72+
json: dict[str, Any] = {"id": id, "name": name or id}
73+
if options:
74+
json.update(**options)
6875
res = await self._request(
6976
"POST",
7077
"/bucket",
71-
json={"id": id, "name": name or id, "public": public},
78+
json=json,
7279
)
7380
return res.json()
7481

82+
async def update_bucket(
83+
self, id: str, options: CreateOrUpdateBucketOptions
84+
) -> dict[str, str]:
85+
"""Update a storage bucket.
86+
87+
Parameters
88+
----------
89+
id
90+
The unique identifier of the bucket you would like to update.
91+
options
92+
The properties you want to update. Valid options are `public`, `file_size_limit` and
93+
`allowed_mime_types`.
94+
"""
95+
json = {"id": id, "name": id, **options}
96+
res = await self._request("PUT", f"/bucket/{id}", json=json)
97+
return res.json()
98+
7599
async def empty_bucket(self, id: str) -> dict[str, str]:
76100
"""Removes all objects inside a single bucket.
77101

storage3/_sync/bucket.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from httpx import HTTPError, Response
66

7-
from ..types import RequestMethod
7+
from ..types import CreateOrUpdateBucketOptions, RequestMethod
88
from ..utils import StorageException, SyncClient
99
from .file_api import SyncBucket
1010

@@ -52,7 +52,10 @@ def get_bucket(self, id: str) -> SyncBucket:
5252
return SyncBucket(**json, _client=self._client)
5353

5454
def create_bucket(
55-
self, id: str, name: Optional[str] = None, public: bool = False
55+
self,
56+
id: str,
57+
name: Optional[str] = None,
58+
options: Optional[CreateOrUpdateBucketOptions] = None,
5659
) -> dict[str, str]:
5760
"""Creates a new storage bucket.
5861
@@ -62,16 +65,37 @@ def create_bucket(
6265
A unique identifier for the bucket you are creating.
6366
name
6467
A name for the bucket you are creating. If not passed, the id is used as the name as well.
65-
public
66-
Whether the bucket you are creating should be publicly accessible. Defaults to False.
68+
options
69+
Extra options to send while creating the bucket. Valid options are `public`, `file_size_limit` and
70+
`allowed_mime_types`.
6771
"""
72+
json: dict[str, Any] = {"id": id, "name": name or id}
73+
if options:
74+
json.update(**options)
6875
res = self._request(
6976
"POST",
7077
"/bucket",
71-
json={"id": id, "name": name or id, "public": public},
78+
json=json,
7279
)
7380
return res.json()
7481

82+
def update_bucket(
83+
self, id: str, options: CreateOrUpdateBucketOptions
84+
) -> dict[str, str]:
85+
"""Update a storage bucket.
86+
87+
Parameters
88+
----------
89+
id
90+
The unique identifier of the bucket you would like to update.
91+
options
92+
The properties you want to update. Valid options are `public`, `file_size_limit` and
93+
`allowed_mime_types`.
94+
"""
95+
json = {"id": id, "name": id, **options}
96+
res = self._request("PUT", f"/bucket/{id}", json=json)
97+
return res.json()
98+
7599
def empty_bucket(self, id: str) -> dict[str, str]:
76100
"""Removes all objects inside a single bucket.
77101

storage3/types.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass
24
from datetime import datetime
35
from typing import Optional, Union
@@ -19,7 +21,7 @@ class BaseBucket:
1921
created_at: datetime
2022
updated_at: datetime
2123
file_size_limit: Optional[int]
22-
allowed_mime_types: Optional[int]
24+
allowed_mime_types: Optional[list[str]]
2325

2426
def __post_init__(self) -> None:
2527
# created_at and updated_at are returned by the API as ISO timestamps
@@ -34,6 +36,12 @@ class _sortByType(TypedDict):
3436
order: Literal["asc", "desc"]
3537

3638

39+
class CreateOrUpdateBucketOptions(TypedDict, total=False):
40+
public: bool
41+
file_size_limit: int
42+
allowed_mime_types: list[str]
43+
44+
3745
class ListBucketFilesOptions(TypedDict):
3846
limit: int
3947
offset: int

tests/_async/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def public_bucket(
8989
global temp_test_buckets_ids
9090
temp_test_buckets_ids.append(bucket_id)
9191

92-
await storage.create_bucket(id=bucket_id, public=True)
92+
await storage.create_bucket(id=bucket_id, options={"public": True})
9393

9494
yield bucket_id
9595

tests/_sync/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def public_bucket(storage: SyncStorageClient, uuid_factory: Callable[[], str]) -
8787
global temp_test_buckets_ids
8888
temp_test_buckets_ids.append(bucket_id)
8989

90-
storage.create_bucket(id=bucket_id, public=True)
90+
storage.create_bucket(id=bucket_id, options={"public": True})
9191

9292
yield bucket_id
9393

0 commit comments

Comments
 (0)