Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toolkit): fix lifecycle in r2/gcp #332

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions projects/fal/src/fal/toolkit/file/providers/fal.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,26 +552,23 @@ def auth_headers(self) -> dict[str, str]:
class FalFileRepositoryV3(FileRepository):
@retry(max_retries=3, base_delay=1, backoff_type="exponential", jitter=True)
def save(
self, file: FileData, user_lifecycle_preference: dict[str, str] | None
self, file: FileData, object_lifecycle_preference: dict[str, str] | None
) -> str:
object_lifecycle_preference = dataclasses.asdict(GLOBAL_LIFECYCLE_PREFERENCE)

if user_lifecycle_preference is not None:
object_lifecycle_preference = {
key: user_lifecycle_preference[key]
if key in user_lifecycle_preference
lifecycle = dataclasses.asdict(GLOBAL_LIFECYCLE_PREFERENCE)
if object_lifecycle_preference is not None:
lifecycle = {
key: object_lifecycle_preference[key]
if key in object_lifecycle_preference
else value
for key, value in object_lifecycle_preference.items()
for key, value in lifecycle.items()
}

headers = {
**self.auth_headers,
"Accept": "application/json",
"Content-Type": file.content_type,
"X-Fal-File-Name": file.file_name,
"X-Fal-Object-Lifecycle-Preference": json.dumps(
object_lifecycle_preference
),
"X-Fal-Object-Lifecycle-Preference": json.dumps(lifecycle),
}
url = os.getenv("FAL_CDN_V3_HOST", _FAL_CDN_V3) + "/files/upload"
request = Request(url, headers=headers, method="POST", data=file.data)
Expand Down
7 changes: 6 additions & 1 deletion projects/fal/src/fal/toolkit/file/providers/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import posixpath
import uuid
from dataclasses import dataclass
from typing import Optional

from fal.toolkit.file.types import FileData, FileRepository
from fal.toolkit.utils.retry import retry
Expand Down Expand Up @@ -52,7 +53,11 @@ def bucket(self):
return self._bucket

@retry(max_retries=3, base_delay=1, backoff_type="exponential", jitter=True)
def save(self, data: FileData) -> str:
def save(
self,
data: FileData,
object_lifecycle_preference: Optional[dict[str, str]] = None,
) -> str:
destination_path = posixpath.join(
self.folder,
f"{uuid.uuid4().hex}_{data.file_name}",
Expand Down
7 changes: 6 additions & 1 deletion projects/fal/src/fal/toolkit/file/providers/r2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uuid
from dataclasses import dataclass
from io import BytesIO
from typing import Optional

from fal.toolkit.file.types import FileData, FileRepository
from fal.toolkit.utils.retry import retry
Expand Down Expand Up @@ -69,7 +70,11 @@ def bucket(self):
return self._bucket

@retry(max_retries=3, base_delay=1, backoff_type="exponential", jitter=True)
def save(self, data: FileData) -> str:
def save(
self,
data: FileData,
object_lifecycle_preference: Optional[dict[str, str]] = None,
) -> str:
destination_path = posixpath.join(
self.key,
f"{uuid.uuid4().hex}_{data.file_name}",
Expand Down
Loading