delete-objects --dry-run option

pull/84/head
Simon Willison 2023-04-30 14:41:55 -07:00
rodzic ae3dd24b1d
commit 20b4545ad1
3 zmienionych plików z 42 dodań i 1 usunięć

Wyświetl plik

@ -121,6 +121,7 @@ Usage: s3-credentials delete-objects [OPTIONS] BUCKET [KEYS]...
Options:
--prefix TEXT Delete everything with this prefix
-s, --silent Don't show informational output
-d, --dry-run Show keys that would be deleted without deleting them
--access-key TEXT AWS access key ID
--secret-key TEXT AWS secret access key
--session-token TEXT AWS session token

Wyświetl plik

@ -1342,8 +1342,15 @@ def get_cors_policy(bucket, **boto_options):
@click.option(
"silent", "-s", "--silent", is_flag=True, help="Don't show informational output"
)
@click.option(
"dry_run",
"-d",
"--dry-run",
is_flag=True,
help="Show keys that would be deleted without deleting them",
)
@common_boto3_options
def delete_objects(bucket, keys, prefix, silent, **boto_options):
def delete_objects(bucket, keys, prefix, silent, dry_run, **boto_options):
"""
Delete one or more object from an S3 bucket
@ -1374,6 +1381,11 @@ def delete_objects(bucket, keys, prefix, silent, **boto_options):
),
err=True,
)
if dry_run:
click.echo("The following keys would be deleted:")
for key in keys:
click.echo(key)
return
for batch in batches(keys, 1000):
response = s3.delete_objects(
Bucket=bucket, Delete={"Objects": [{"Key": key} for key in batch]}

Wyświetl plik

@ -1248,3 +1248,31 @@ def test_delete_objects(moto_s3_populated, args, expected, expected_error):
or []
}
assert keys == set(expected)
@pytest.mark.parametrize("arg", ("-d", "--dry-run"))
def test_delete_objects_dry_run(moto_s3_populated, arg):
runner = CliRunner(mix_stderr=False)
def get_keys():
return {
obj["Key"]
for obj in moto_s3_populated.list_objects(Bucket="my-bucket").get(
"Contents"
)
or []
}
with runner.isolated_filesystem():
before_keys = get_keys()
result = runner.invoke(
cli, ["delete-objects", "my-bucket", "--prefix", "directory/", arg]
)
assert result.exit_code == 0
assert result.output == (
"The following keys would be deleted:\n"
"directory/three.json\n"
"directory/two.txt\n"
)
after_keys = get_keys()
assert before_keys == after_keys