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

Add optional folder argument for file uploads to S3 Buckets #208

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion acquire/uploaders/minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, upload: dict[str, str], **kwargs: dict[str, Any]) -> None:
self.access_id = upload.get("access_id")
self.access_key = upload.get("access_key")
self.bucket_name = upload.get("bucket")
self.folder = upload.get("folder", "").rstrip("/")

if not all((self.endpoint, self.access_id, self.access_key, self.bucket_name)):
raise ValueError("Invalid cloud upload configuration")
Expand All @@ -45,7 +46,11 @@ def prepare_client(self, paths: list[Path], proxies: Optional[dict[str, str]] =
return Minio(self.endpoint, self.access_id, self.access_key, http_client=http_client)

def upload_file(self, client: Any, path: Path) -> None:
client.fput_object(self.bucket_name, os.path.basename(path), path)
object_path = path.name
if self.folder:
object_path = f"{self.folder}/{object_path}"

client.fput_object(self.bucket_name, object_path, path)

def finish(self, client: Any) -> None:
pass
33 changes: 33 additions & 0 deletions tests/test_minio_uploader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import Callable
from unittest.mock import Mock, patch

import pytest
Expand Down Expand Up @@ -79,3 +80,35 @@ def test_upload_file_multiple_failures(minio_instance: MinIO):
with patch("acquire.uploaders.plugin.log") as mocked_logger:
upload_files_using_uploader(minio_instance, [Path("hello")])
mocked_logger.error.assert_called_with("Upload %s FAILED after too many attempts. Stopping.", Path("hello"))


def test_minio_folder_initialization(minio_plugin: Callable) -> None:
arguments = {"endpoint": "test", "access_id": "test", "access_key": "test", "bucket": "test", "folder": "Uploads/"}
minio = minio_plugin(upload=arguments)
assert minio.folder == "Uploads"

arguments["folder"] = "Uploads/test_folder"
minio_no_backslash = minio_plugin(upload=arguments)
assert minio_no_backslash.folder == "Uploads/test_folder"

arguments.pop("folder")
minio_no_folder = minio_plugin(upload=arguments)
assert minio_no_folder.folder == ""


def test_upload_file_with_folder(minio_instance: MinIO):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_upload_file_with_folder(minio_instance: MinIO):
def test_upload_file_with_folder(minio_instance: MinIO) -> None:

mock_client = Mock()
test_path = Path("example.txt")
minio_instance.folder = "test_folder"
minio_instance.upload_file(mock_client, test_path)

mock_client.fput_object.assert_called_once_with("test", "test_folder/example.txt", test_path)


def test_upload_file_without_folder(minio_instance: MinIO):
mock_client = Mock()
test_path = Path("example.txt")
minio_instance.folder = ""
minio_instance.upload_file(mock_client, test_path)

mock_client.fput_object.assert_called_once_with("test", "example.txt", test_path)
Loading