-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add object storage support for PDF and Image extraction #88
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
Merged
gomesrocha
merged 1 commit into
main
from
refactor/object-storage-support-7752691853998699012
Feb 17, 2026
Merged
Changes from all commits
Commits
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
Empty file.
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,40 @@ | ||
| import json | ||
| import os | ||
| from typing import Dict, Optional | ||
| from pydantic import BaseModel | ||
|
|
||
| class TenantConfig(BaseModel): | ||
| bucket_name: str | ||
| endpoint_url: Optional[str] = None | ||
| aws_access_key_id: str | ||
| aws_secret_access_key: str | ||
| region_name: str | ||
|
|
||
| # Cache for tenant configuration | ||
| _TENANTS_CACHE: Dict[str, TenantConfig] = {} | ||
|
|
||
| def load_tenants(config_path: str = "tenants.json"): | ||
| """ | ||
| Load tenant configurations from a JSON file. | ||
| """ | ||
| global _TENANTS_CACHE | ||
| if not os.path.exists(config_path): | ||
| return | ||
|
|
||
| try: | ||
| with open(config_path, "r") as f: | ||
| data = json.load(f) | ||
| for client_id, config in data.items(): | ||
| _TENANTS_CACHE[client_id] = TenantConfig(**config) | ||
| except Exception as e: | ||
| # In a real app, logging here would be good | ||
| print(f"Error loading tenants configuration: {e}") | ||
|
|
||
| def get_tenant_config(client_id: str) -> Optional[TenantConfig]: | ||
| """ | ||
| Retrieve configuration for a specific tenant. | ||
| Initializes cache if empty. | ||
| """ | ||
| if not _TENANTS_CACHE: | ||
| load_tenants() | ||
| return _TENANTS_CACHE.get(client_id) |
Empty file.
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,55 @@ | ||
| import boto3 | ||
| import tempfile | ||
| import os | ||
| import logging | ||
| from botocore.exceptions import ClientError | ||
| from app.core.tenants import get_tenant_config | ||
|
|
||
| log = logging.getLogger("uvicorn") | ||
|
|
||
| def download_file_from_storage(client_id: str, object_key: str) -> str: | ||
| """ | ||
| Downloads a file from object storage to a temporary file. | ||
|
|
||
| Args: | ||
| client_id (str): The tenant identifier. | ||
| object_key (str): The path to the file in the bucket. | ||
|
|
||
| Returns: | ||
| str: The path to the downloaded temporary file. | ||
|
|
||
| Raises: | ||
| ValueError: If tenant configuration is missing. | ||
| ClientError: If S3 interaction fails (e.g. file not found). | ||
| """ | ||
| config = get_tenant_config(client_id) | ||
| if not config: | ||
| raise ValueError(f"Configuration for client {client_id} not found.") | ||
|
|
||
| try: | ||
| # Determine file extension if possible | ||
| suffix = os.path.splitext(object_key)[1] | ||
|
|
||
| # Create a temporary file (not automatically deleted on close, we delete it later) | ||
| # Using delete=False so we can return the path and use it. | ||
| with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file: | ||
| log.info(f"Downloading {object_key} for client {client_id} from {config.endpoint_url or 'AWS S3'}...") | ||
|
|
||
| s3_client = boto3.client( | ||
| "s3", | ||
| endpoint_url=config.endpoint_url, | ||
| aws_access_key_id=config.aws_access_key_id, | ||
| aws_secret_access_key=config.aws_secret_access_key, | ||
| region_name=config.region_name | ||
| ) | ||
|
|
||
| s3_client.download_fileobj(config.bucket_name, object_key, tmp_file) | ||
| log.info(f"Downloaded to {tmp_file.name}") | ||
| return tmp_file.name | ||
|
|
||
| except ClientError as e: | ||
| log.error(f"S3 ClientError downloading {object_key}: {e}") | ||
Check noticeCode scanning / SonarCloud Logging should not be vulnerable to injection attacks Low
Change this code to not log user-controlled data. See more on SonarQube Cloud
|
||
| raise e | ||
| except Exception as e: | ||
| log.error(f"Unexpected error downloading {object_key}: {e}") | ||
Check noticeCode scanning / SonarCloud Logging should not be vulnerable to injection attacks Low
Change this code to not log user-controlled data. See more on SonarQube Cloud
|
||
| raise e | ||
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,9 @@ | ||
| { | ||
| "client_a": { | ||
| "bucket_name": "bucket-a", | ||
| "endpoint_url": "https://s3.example.com", | ||
| "aws_access_key_id": "mock_key", | ||
| "aws_secret_access_key": "mock_secret", | ||
| "region_name": "us-east-1" | ||
| } | ||
| } |
Oops, something went wrong.
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.
Check failure
Code scanning / SonarCloud
S3 operations should verify bucket ownership using ExpectedBucketOwner parameter High