Saq*_*Ali 14 amazon-s3 amazon-web-services aws-cli
假设我有一个名为的S3存储桶 x.y.z
在这个桶中,我有数百个文件.但我只想删除2个名为purple.gif和的文件worksheet.xlsx
我可以通过一次调用从AWS命令行工具执行此操作rm吗?
这不起作用:
$ aws s3 rm s3://x.y.z/worksheet.xlsx s3://x.y.z/purple.gif
Unknown options: s3://x.y.z/purple.gif
Run Code Online (Sandbox Code Playgroud)
从手册中,您似乎无法按名称明确删除文件列表.有谁知道这样做的方法?我不喜欢使用--recursive旗帜.
Kha*_* T. 38
您可以通过多次提供--exclude或--include参数来完成此操作.但是,你必须使用--recursive它来工作.
如果有多个过滤器,请记住过滤器参数的顺序很重要.规则是命令中稍后出现的过滤器优先于命令中较早出现的过滤器.
aws s3 rm s3://x.y.z/ --recursive --exclude "*" --include "purple.gif" --include "worksheet.xlsx"
Run Code Online (Sandbox Code Playgroud)
在这里,除了purple.gif和worksheet.xlsx之外,所有文件都将从命令中排除.
来源:使用排除和包含过滤器
spg*_*spg 14
你不能使用s3 rm,但你可以使用s3api delete-objects:
aws s3api delete-objects --bucket x.y.z --delete '{"Objects":[{"Key":"worksheet.xlsx"},{"Key":"purple.gif"}]}'
Run Code Online (Sandbox Code Playgroud)
Thy*_*yag 10
显然 aws s3 rm 仅适用于单个文件/对象。
下面是一个 bash 命令,它构造单独的删除命令,然后一个一个地删除对象。取得了一些成功(可能有点慢,但有效):
aws s3 ls s3://bucketname/foldername/ |
awk {'print "aws s3 rm s3://bucketname/foldername/" $4'} |
bash
Run Code Online (Sandbox Code Playgroud)
前两行用于构建“rm”命令,第三行 (bash) 将执行它们。
请注意,如果您的对象名称包含空格或有趣的字符,您可能会遇到问题。这是因为“aws s3 ls”命令不会列出此类对象(截至撰写本文时)
aws s3 rm s3://<bucketname>/2023/ --recursive --exclude '*' --include 'A*.csv'
Run Code Online (Sandbox Code Playgroud)
上面的答案都没有提到如何使用通配符表达式来选择多个文件并删除。
如果您的用例是使用命名模式选择 S3 中的多个对象并删除,则上述命令将很有用
在AWS S3中使用UNIX WILDCARDS(AWS CLI)
当前,AWS CLI在命令的“ path”参数中不提供对UNIX通配符的支持。但是,使用几个aws s3命令上可用的--exclude和--include参数来复制此功能非常容易。
可用的通配符为:
“ *” –匹配所有内容
“?” –匹配任何单个字符
“ []” –匹配方括号之间的任何单个字符
“ [!]” –匹配括号之间的任何单个字符
关于在aws s3命令中使用--include和--exclude的几点注意事项:
您可以使用任意数量的--include和--exclude参数。
稍后传递的参数优先于先前传递的参数(在同一命令中)。
默认情况下,所有文件和对象都是“ 包括 ”的,因此,为了仅包括某些文件,您必须使用“排除”然后“包括”。--recursive必须与--include和--exclude结合使用,否则命令将仅执行单个文件/对象操作。
示例: 将所有文件从工作目录复制到大基准存储桶:
aws s3 cp ./ s3://big-datums/ --recursive
从大基准存储桶中删除所有“ .java”文件:
aws s3 rm s3://big-datums/ --recursive --exclude "*" --include "*.java"
删除大基准段中所有扩展名为“ j”或“ c”(“。csv”,“。java,“。json”,。“ jpeg”等)的文件:
aws s3 rm s3://big-datums/ --recursive --exclude "*" --include "*.[jc]*"
将“ .txt”和“ .csv”文件从大基准S3存储桶复制到本地工作目录:
aws s3 cp s3://big-datums/ . --recursive --exclude "*" --include "*.txt" --include "*.csv"
#Copy all files from working directory to the big-datums bucket:
aws s3 cp ./ s3://big-datums/ --recursive
#Delete all ".java" files from the big-datums bucket:
aws s3 rm s3://big-datums/ --recursive --exclude "*" --include "*.java"
#Delete all files in the big-datums bucket with a file extension beginning with "j" or "c" (".csv", ".java, ".json", ."jpeg", etc.):
aws s3 rm s3://big-datums/ --recursive --exclude "*" --include "*.[jc]*"
#Copy ".txt" and ".csv" files from big-datums S3 bucket to local working directory:
aws s3 cp s3://big-datums/ . --recursive --exclude "*" --include "*.txt" --include "*.csv" ```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20698 次 |
| 最近记录: |