如何根据另一个S3存储桶中的内容删除S3存储桶中的对象?

问题描述 投票:0回答:1

上下文:我有 2 个存储桶,存储桶 A 和存储桶 B。存储桶 A 将其所有内容通过

aws s3 sync
CLI 命令放置在存储桶 B 中。

问题:我想删除存储桶B中也存在于存储桶A中的所有项目,而不删除存储桶A中的任何内容。

例如

桶A(来源):

  1. 文件R
  2. 文件G
  3. 文件C

B桶(目的地):

  1. 文件A
  2. 文件R
  3. 文件G
  4. 文件C
  5. 文件O

^^ 我需要删除目标目的地中所有do存在于源目的地中的文件,因此仅需要从B桶中删除文件R、G和C。

尝试的解决方案:

aws s3 sync
CLI 命令包含标志
--delete
。但是,此标志仅确保删除目标目的地中不在源目的地中的任何文件。

有什么方法可以使用

aws s3 sync
来做到这一点吗?

amazon-web-services amazon-s3 command-line-interface boto3 bucket
1个回答
0
投票

要进行一次性清理,请使用下面的 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.
© www.soinside.com 2019 - 2024. All rights reserved.