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

31 fixing fget object #32

Merged
merged 2 commits into from
May 19, 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
32 changes: 29 additions & 3 deletions pytest_minio_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""
import copy
import datetime
import errno
import io
import logging
import os
Expand Down Expand Up @@ -821,7 +822,7 @@ def fget_object(
object_name,
file_path,
request_headers=None,
sse=None,
ssec=None,
version_id=None,
extra_query_params=None,
):
Expand Down Expand Up @@ -851,18 +852,43 @@ def fget_object(
IOError: If there's an issue writing to the specified file path.

Returns:
None: The method writes the object's data to a file and has no return value.
Object: Stat of the object as Object.
"""

stat = self.stat_object(
bucket_name,
object_name,
ssec,
version_id,
)

the_object = self.get_object(
bucket_name,
object_name,
version_id=version_id,
request_headers=request_headers,
ssec=ssec,
version_id=version_id,
extra_query_params=extra_query_params,
)

if os.path.isdir(file_path):
raise ValueError(f"file {file_path} is a directory")

# Create top level directory if needed.

dirname = os.path.dirname(file_path)
if dirname:
try:
os.makedirs(dirname)
except OSError as exc: # Python >2.5
if exc.errno != errno.EEXIST:
raise

with open(file_path, "wb") as f:
f.write(the_object.data)

return stat

def get_object(
self,
bucket_name,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_minio_mock.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import sys

import pytest
import validators
from minio import Minio
from minio.commonconfig import ENABLED
from minio.datatypes import Bucket
from minio.datatypes import Object
from minio.error import S3Error
from minio.versioningconfig import OFF
from minio.versioningconfig import SUSPENDED
Expand Down Expand Up @@ -493,3 +495,34 @@ def test_stat_object(minio_mock):
version_id=objects[1].version_id,
)
assert object_stat.version_id is None


@pytest.mark.REGRESSION
@pytest.mark.UNIT
def test_fget_object(minio_mock, tmp_path):
client = Minio("http://local.host:9000")

bucket_name = "new-bucket"
client.make_bucket(bucket_name)
objects = client.list_objects(bucket_name)
assert len(list(objects)) == 0

client.put_object(bucket_name, "object1", data=b"object1 data", length=12)

file_path = tmp_path # should raise a Value error
with pytest.raises(ValueError):
_ = client.fget_object(bucket_name, "object1", file_path)

file_path = os.path.join(tmp_path, "object1.dat")
stat = client.fget_object(bucket_name, "object1", file_path)
assert isinstance(stat, Object)

# folder objects does not exist, fget_object should create it
file_path = os.path.join(tmp_path, "another_folder", "object1.dat")
stat = client.fget_object(bucket_name, "object1", file_path)
assert isinstance(stat, Object)

# folder objects does not exist, fget_object should create it
file_path = os.path.join(tmp_path, "another_folder", "object1.dat")
stat = client.fget_object(bucket_name, "object1", file_path)
assert isinstance(stat, Object)
Loading