-
Notifications
You must be signed in to change notification settings - Fork 518
Add S3 File Provider #2164
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
Open
NolanTrem
wants to merge
7
commits into
main
Choose a base branch
from
Nolan/S3FileProvider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add S3 File Provider #2164
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
856eb9c
Break out File Provider
NolanTrem 4a4c43c
Pre-commit
NolanTrem 896a82e
Lint
NolanTrem 4fef3bc
Add validate_config method
NolanTrem 17a7a6d
Add mock_file to tests
NolanTrem ceb9bd0
S3 implementation
NolanTrem 394f1b9
Add minio to docker
NolanTrem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MINIO_ROOT_USER=minioadmin | ||
MINIO_ROOT_PASSWORD=minioadmin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import logging | ||
import os | ||
from abc import ABC, abstractmethod | ||
from datetime import datetime | ||
from io import BytesIO | ||
from typing import BinaryIO, Optional | ||
from uuid import UUID | ||
|
||
from .base import Provider, ProviderConfig | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
class FileConfig(ProviderConfig): | ||
""" | ||
Configuration for file storage providers. | ||
""" | ||
|
||
provider: Optional[str] = None | ||
|
||
# S3-specific configuration | ||
bucket_name: Optional[str] = None | ||
aws_access_key_id: Optional[str] = None | ||
aws_secret_access_key: Optional[str] = None | ||
region_name: Optional[str] = None | ||
endpoint_url: Optional[str] = None | ||
|
||
@property | ||
def supported_providers(self) -> list[str]: | ||
""" | ||
List of supported file storage providers. | ||
""" | ||
return [ | ||
"postgres", | ||
"s3", | ||
] | ||
|
||
def validate_config(self) -> None: | ||
if self.provider not in self.supported_providers: | ||
raise ValueError(f"Unsupported file provider: {self.provider}") | ||
|
||
if self.provider == "s3" and ( | ||
not self.bucket_name and not os.getenv("S3_BUCKET_NAME") | ||
): | ||
raise ValueError( | ||
"S3 bucket name is required when using S3 provider" | ||
) | ||
|
||
|
||
class FileProvider(Provider, ABC): | ||
""" | ||
Base abstract class for file storage providers. | ||
""" | ||
|
||
def __init__(self, config: FileConfig): | ||
if not isinstance(config, FileConfig): | ||
raise ValueError( | ||
"FileProvider must be initialized with a `FileConfig`." | ||
) | ||
super().__init__(config) | ||
self.config: FileConfig = config | ||
|
||
@abstractmethod | ||
async def initialize(self) -> None: | ||
"""Initialize the file provider.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def store_file( | ||
self, | ||
document_id: UUID, | ||
file_name: str, | ||
file_content: BytesIO, | ||
file_type: Optional[str] = None, | ||
) -> None: | ||
"""Store a file.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def retrieve_file( | ||
self, document_id: UUID | ||
) -> Optional[tuple[str, BinaryIO, int]]: | ||
"""Retrieve a file.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def retrieve_files_as_zip( | ||
self, | ||
document_ids: Optional[list[UUID]] = None, | ||
start_date: Optional[datetime] = None, | ||
end_date: Optional[datetime] = None, | ||
) -> tuple[str, BinaryIO, int]: | ||
"""Retrieve multiple files as a zip.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def delete_file(self, document_id: UUID) -> bool: | ||
"""Delete a file.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def get_files_overview( | ||
self, | ||
offset: int, | ||
limit: int, | ||
filter_document_ids: Optional[list[UUID]] = None, | ||
filter_file_names: Optional[list[str]] = None, | ||
) -> list[dict]: | ||
"""Get an overview of stored files.""" | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A shot in the dark here, but should this be "supabase"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll keep this to
s3
, since it should support any s3 compatible solution (not just Supabase).