Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added pagination and filtering to s3 list buckets #12370

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

Closed
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
48 changes: 42 additions & 6 deletions localstack-core/localstack/services/s3/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,50 @@ def list_buckets(
bucket_region: BucketRegion = None,
**kwargs,
) -> ListBucketsOutput:
# TODO add support for max_buckets, continuation_token, prefix, and bucket_region
if continuation_token == "":
raise InvalidArgument(
"The continuation token provided is incorrect",
ArgumentName="continuation-token",
)

owner = get_owner_for_account_id(context.account_id)
store = self.get_store(context.account_id, context.region)
buckets = [
Bucket(Name=bucket.name, CreationDate=bucket.creation_date)
for bucket in store.buckets.values()
]
return ListBucketsOutput(Owner=owner, Buckets=buckets)

decoded_continuation_token = (
to_str(base64.urlsafe_b64decode(continuation_token.encode()))
if continuation_token
else None
)

count = 0
buckets: list[Bucket] = []
next_continuation_token = None

for bucket in sorted(store.buckets.values(), key=lambda r: r.name):
if continuation_token and bucket.name < decoded_continuation_token:
continue

if prefix and not bucket.name.startswith(prefix):
continue

if bucket_region and not bucket.bucket_region == bucket_region:
continue

if max_buckets and count >= max_buckets:
next_continuation_token = to_str(base64.urlsafe_b64encode(bucket.name.encode()))
break

output_bucket = Bucket(
Name=bucket.name,
CreationDate=bucket.creation_date,
BucketRegion=bucket.bucket_region,
)
buckets.append(output_bucket)
count += 1

return ListBucketsOutput(
Owner=owner, Buckets=buckets, Prefix=prefix, ContinuationToken=next_continuation_token
)

def head_bucket(
self,
Expand Down