Skip to content

Commit

Permalink
list-bucket --urls option, closes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 5, 2024
1 parent 1628856 commit 6b0bfe7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/other-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ You can use the `--prefix myprefix/` option to list only keys that start with a

The commmand accepts the same `--nl`, `--csv` and `--tsv` options as `list-users`.

Add `--urls` to include a `URL` field in the output providing the full URL to each object.

## list-user-policies

To see a list of inline policies belonging to users:
Expand Down
22 changes: 19 additions & 3 deletions s3_credentials/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,10 @@ def ensure_s3_role_exists(iam, sts):
@cli.command()
@click.argument("bucket")
@click.option("--prefix", help="List keys starting with this prefix")
@click.option("--urls", is_flag=True, help="Show URLs for each key")
@common_output_options
@common_boto3_options
def list_bucket(bucket, prefix, nl, csv, tsv, **boto_options):
def list_bucket(bucket, prefix, urls, nl, csv, tsv, **boto_options):
"""
List contents of bucket
Expand All @@ -949,16 +950,31 @@ def list_bucket(bucket, prefix, nl, csv, tsv, **boto_options):
Add --csv or --csv for CSV or TSV format:
s3-credentials list-bucket my-bucket --csv
Add --urls to get an extra URL field for each key:
s3-credentials list-bucket my-bucket --urls
"""
s3 = make_client("s3", **boto_options)
kwargs = {"Bucket": bucket}
if prefix:
kwargs["Prefix"] = prefix

fields = ["Key", "LastModified", "ETag", "Size", "StorageClass", "Owner"]
if urls:
fields.append("URL")

items = paginate(s3, "list_objects_v2", "Contents", **kwargs)
if urls:
items = (
dict(item, URL="https://s3.amazonaws.com/{}/{}".format(bucket, item["Key"]))
for item in items
)

try:
output(
paginate(s3, "list_objects_v2", "Contents", **kwargs),
("Key", "LastModified", "ETag", "Size", "StorageClass", "Owner"),
items,
fields,
nl,
csv,
tsv,
Expand Down

0 comments on commit 6b0bfe7

Please sign in to comment.