Skip to content

Commit

Permalink
merge list_objects_v2() into list_objects() method
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed Aug 12, 2020
1 parent e1d58b2 commit c7e4734
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 332 deletions.
2 changes: 1 addition & 1 deletion minio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

__title__ = 'minio-py'
__author__ = 'MinIO, Inc.'
__version__ = '6.0.1'
__version__ = '7.0.0'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2015, 2016, 2017, 2018, 2019, 2020 MinIO, Inc.'

Expand Down
71 changes: 14 additions & 57 deletions minio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,53 +1039,8 @@ def put_object(self, bucket_name, object_name, data, length,
progress=progress)

def list_objects(self, bucket_name, prefix=None, recursive=False,
include_version=False):
"""
Lists object information of a bucket using S3 API version 1, optionally
for prefix recursively.
:param bucket_name: Name of the bucket.
:param prefix: Object name starts with prefix.
:param recursive: List recursively than directory structure emulation.
:param include_version: Flag to control whether include object
versions.
:return: An iterator contains object information.
Example::
# List objects information.
objects = minio.list_objects('foo')
for object in objects:
print(object)
# List objects information those names starts with 'hello/'.
objects = minio.list_objects('foo', prefix='hello/')
for object in objects:
print(object)
# List objects information recursively.
objects = minio.list_objects('foo', recursive=True)
for object in objects:
print(object)
# List objects information recursively those names starts with
# 'hello/'.
objects = minio.list_objects(
'foo', prefix='hello/', recursive=True,
)
for object in objects:
print(object)
"""
return self._list_objects(
bucket_name,
delimiter=None if recursive else "/",
prefix=prefix,
use_version1=True,
include_version=include_version,
)

def list_objects_v2(self, bucket_name, prefix=None, recursive=False,
start_after=None, include_user_meta=False,
include_version=False):
start_after=None, include_user_meta=False,
include_version=False, use_api_v1=False):
"""
Lists object information of a bucket using S3 API version 2, optionally
for prefix recursively.
Expand All @@ -1098,35 +1053,36 @@ def list_objects_v2(self, bucket_name, prefix=None, recursive=False,
user metadata.
:param include_version: Flag to control whether include object
versions.
:param use_api_v1: Flag to control to use ListObjectV1 S3 API or not.
:return: An iterator contains object information.
Example::
# List objects information.
objects = minio.list_objects_v2('foo')
objects = minio.list_objects('foo')
for object in objects:
print(object)
# List objects information those names starts with 'hello/'.
objects = minio.list_objects_v2('foo', prefix='hello/')
objects = minio.list_objects('foo', prefix='hello/')
for object in objects:
print(object)
# List objects information recursively.
objects = minio.list_objects_v2('foo', recursive=True)
objects = minio.list_objects('foo', recursive=True)
for object in objects:
print(object)
# List objects information recursively those names starts with
# 'hello/'.
objects = minio.list_objects_v2(
objects = minio.list_objects(
'foo', prefix='hello/', recursive=True,
)
for object in objects:
print(object)
# List objects information recursively after object name
# 'hello/world/1'.
objects = minio.list_objects_v2(
objects = minio.list_objects(
'foo', recursive=True, start_after='hello/world/1',
)
for object in objects:
Expand All @@ -1138,6 +1094,7 @@ def list_objects_v2(self, bucket_name, prefix=None, recursive=False,
include_user_meta=include_user_meta,
prefix=prefix,
start_after=start_after,
use_api_v1=use_api_v1,
include_version=include_version,
)

Expand Down Expand Up @@ -2151,7 +2108,7 @@ def _list_objects( # pylint: disable=too-many-arguments,too-many-branches
prefix=None, # all
start_after=None, # all: v1:marker, versioned:key_marker
version_id_marker=None, # versioned
use_version1=False,
use_api_v1=False,
include_version=False,
):
"""
Expand All @@ -2172,10 +2129,10 @@ def _list_objects( # pylint: disable=too-many-arguments,too-many-branches
query = {}
if include_version:
query["versions"] = ""
elif not use_version1:
elif not use_api_v1:
query["list-type"] = "2"

if not include_version and not use_version1:
if not include_version and not use_api_v1:
if continuation_token:
query["continuation-token"] = continuation_token
if fetch_owner:
Expand All @@ -2190,7 +2147,7 @@ def _list_objects( # pylint: disable=too-many-arguments,too-many-branches
if start_after:
if include_version:
query["key-marker"] = start_after
elif use_version1:
elif use_api_v1:
query["marker"] = start_after
else:
query["start-after"] = start_after
Expand All @@ -2207,7 +2164,7 @@ def _list_objects( # pylint: disable=too-many-arguments,too-many-branches
objects, is_truncated, start_after, version_id_marker = (
parse_list_object_versions(response.data, bucket_name)
)
elif use_version1:
elif use_api_v1:
objects, is_truncated, start_after = parse_list_objects(
response.data,
bucket_name,
Expand Down
42 changes: 19 additions & 23 deletions tests/functional/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ def test_get_partial_object(log_entry, sse=None):
_CLIENT.remove_bucket(bucket_name)


def _test_list_objects(log_entry, version2=False, version_check=False):
def _test_list_objects(log_entry, use_api_v1=False, version_check=False):
"""Test list_objects()."""

# Get a unique bucket_name and object_name
Expand Down Expand Up @@ -1092,14 +1092,10 @@ def _test_list_objects(log_entry, version2=False, version_check=False):
bucket_name, object_name + "-2", LimitedRandomReader(size), size,
)
# List all object paths in bucket.
if version2:
objects = _CLIENT.list_objects_v2(
bucket_name, '', is_recursive, include_version=version_check,
)
else:
objects = _CLIENT.list_objects(
bucket_name, '', is_recursive, include_version=version_check,
)
objects = _CLIENT.list_objects(
bucket_name, '', is_recursive, include_version=version_check,
use_api_v1=use_api_v1,
)
for obj in objects:
_ = (obj.bucket_name, obj.object_name, obj.last_modified,
obj.etag, obj.size, obj.content_type)
Expand All @@ -1119,14 +1115,14 @@ def _test_list_objects(log_entry, version2=False, version_check=False):
_CLIENT.remove_bucket(bucket_name)


def test_list_objects(log_entry):
def test_list_objects_v1(log_entry):
"""Test list_objects()."""
_test_list_objects(log_entry)
_test_list_objects(log_entry, use_api_v1=True)


def test_list_object_versions(log_entry):
def test_list_object_v1_versions(log_entry):
"""Test list_objects()."""
_test_list_objects(log_entry, version_check=True)
_test_list_objects(log_entry, use_api_v1=True, version_check=True)


def _test_list_objects_api(bucket_name, expected_no, *argv):
Expand Down Expand Up @@ -1266,14 +1262,14 @@ def test_list_objects_with_1001_files( # pylint: disable=invalid-name
_CLIENT.remove_bucket(bucket_name)


def test_list_objects_v2(log_entry):
"""Test list_objects_v2()."""
_test_list_objects(log_entry, version2=True)
def test_list_objects(log_entry):
"""Test list_objects()."""
_test_list_objects(log_entry)


def test_list_object_versions_v2(log_entry):
"""Test list_objects_v2() of versioned object."""
_test_list_objects(log_entry, version2=True, version_check=True)
def test_list_object_versions(log_entry):
"""Test list_objects() of versioned object."""
_test_list_objects(log_entry, version_check=True)


def _create_upload_ids(bucket_name, object_name, count):
Expand Down Expand Up @@ -1982,13 +1978,13 @@ def main():
test_fget_object_version: {"sse": ssec} if ssec else None,
test_get_object_with_default_length: None,
test_get_partial_object: {"sse": ssec} if ssec else None,
test_list_objects: None,
test_list_object_versions: None,
test_list_objects_v1: None,
test_list_object_v1_versions: None,
test_list_objects_with_prefix: None,
test_list_objects_with_1001_files: None,
test_remove_incomplete_upload: None,
test_list_objects_v2: None,
test_list_object_versions_v2: None,
test_list_objects: None,
test_list_object_versions: None,
test_presigned_get_object_default_expiry: None,
test_presigned_get_object_expiry: None,
test_presigned_get_object_response_headers: None,
Expand Down
Loading

0 comments on commit c7e4734

Please sign in to comment.