上下文:我有 2 个存储桶,存储桶 A 和存储桶 B。存储桶 A 将其所有内容通过
aws s3 sync
CLI 命令放置在存储桶 B 中。
问题:我想删除存储桶B中也存在于存储桶A中的所有项目,而不删除存储桶A中的任何内容。
例如
桶A(来源):
B桶(目的地):
^^ 我需要删除目标目的地中所有do存在于源目的地中的文件,因此仅需要从B桶中删除文件R、G和C。
尝试的解决方案:
aws s3 sync
CLI 命令包含标志 --delete
。但是,此标志仅确保删除目标目的地中不在源目的地中的任何文件。
有什么方法可以使用
aws s3 sync
来做到这一点吗?
要进行一次性清理,请使用下面的 shell 命令序列。首先使用
--dryrun
标志运行它,然后,如果输出看起来符合预期,则不使用该标志。
aws s3 ls s3://bucket_a/ | tr -s ' ' | cut -d' ' -f 4 | xargs -t -I % aws s3 rm --dryrun s3://bucket_b/%
每个命令的解释如下。
aws s3 ls s3://bucket_a/ # List files in the original location.
tr -s ' ' # Remove duplicate whitespaces for the cut command.
cut -d' ' -f 4 # Extract file names.
xargs -t -I % aws s3 rm --dryrun s3://bucket_b/% # Execute rm for each file name in the location with the copies.