Skip to content

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

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
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 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,12 @@ 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
43 changes: 43 additions & 0 deletions tests/test_minio_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,46 @@ 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):
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_minio_folder_initialization(minio_plugin):
def test_minio_folder_initialization(minio_plugin: Callable) -> None:

Callable should probably be imported at the top first.

arguments = {
"endpoint": "test",
"access_id": "test",
"access_key": "test",
"bucket": "test",
"folder": "Uploads/"
}
minio = minio_plugin(upload=arguments)
assert minio.folder == "Uploads"

minio.folder = "Uploads/test_folder"
assert minio.folder == "Uploads/test_folder"
Copy link
Contributor

Choose a reason for hiding this comment

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

This part only tests the assignment of the folder attribute, not what happens during the __init__ of the object. I think this would test the thing you where after

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're totally right, this is what I meant to do ;)


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()
minio_instance.folder = ""
mock_client = Mock()
test_path = Path("example.txt")
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
mock_client = Mock()
minio_instance.folder = ""
mock_client = Mock()
test_path = Path("example.txt")
mock_client = Mock()
test_path = Path("example.txt")
minio_instance.folder = ""

for consistency with the other test :)

minio_instance.upload_file(mock_client, test_path)

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