如何使用 AWS CLI 仅复制 S3 存储桶中与给定字符串模式匹配的文件

How to use AWS CLI to only copy files in S3 bucket that match a given string pattern

我正在使用 AWS CLI 使用如下命令将文件从 S3 存储桶复制到我的 R 机器:

  system(
    "aws s3 cp s3://my_bucket_location/ ~/my_r_location/ --recursive --exclude '*' --include '*trans*' --region us-east-1"
    )

这按预期工作,即它复制 my_bucket_location 中该位置文件名中具有 "trans" 的所有文件。

我面临的问题是我不想在此步骤中导入具有类似命名约定的其他文件。例如,在下面的列表中,我只想复制前两个文件,而不是后两个:

File list
trans_120215.csv
trans_130215.csv
sum_trans_120215.csv
sum_trans_130215.csv

如果我使用的是正则表达式,我可以使其更具体,例如 "^trans_\d+" 以仅引入前两个文件,但这似乎无法使用 AWS CLI。所以我的问题是有没有一种方法可以像下面这样使用 AWS CLI 进行更复杂的模式匹配?

  system(
    "aws s3 cp s3://my_bucket_location/ ~/my_r_location/ --recursive --exclude '*' --include '^trans_\d+' --region us-east-1"
    )

请注意,我只能使用有关该文件的信息,即我想导入具有模式 "^trans_\d+" 的文件,我不能使用其他不需要的文件包含 sum_ 的事实开始,因为这只是一个示例,可能还有其他具有类似名称的文件,例如 "check_trans_120215.csv".

我已经考虑过如下其他替代方案,但希望有一种方法可以调整复制命令以避免沿着这些路线走下去:

您列出的备选方案是最佳选择,因为 S3 CLI 不支持 regex

Use of Exclude and Include Filters:

Currently, there is no support for the use of UNIX style wildcards in a command's path arguments. However, most commands have --exclude "" and --include "" parameters that can achieve the desired result. These parameters perform pattern matching to either exclude or include a particular file or object. The following pattern symbols are supported.

*: Matches everything
?: Matches any single character
[sequence]: Matches any character in sequence
[!sequence]: Matches any character not in sequence

把它放在这里是为了让其他人可以找到,因为我只是想弄明白。这是我想出的:

s3cmd del $(s3cmd ls s3://[BUCKET]/ | grep '.*s3://[BUCKET]/[FILENAME]' | cut -c 41-)

您可以将正则表达式放在 grep 搜索字符串中。例如,我正在搜索要删除的特定文件(因此使用 s3cmd del)。我的正则表达式看起来像:'2016-11-04.*s3.*[DN][RS].*'。您可能需要调整切割以供您使用。还应该与 s3cmd get.

一起使用