AWS CLI S3同步:如何排除多个文件 [英] aws cli s3 sync: how to exclude multiple files
问题描述
因此,我有一个bash脚本,该脚本从AWS S3存储桶中删除所有内容,然后将本地文件夹的内容上传到该存储桶中.
So I have a bash script which deletes all contents from an AWS S3 bucket and then uploads the contents of a local folder to that same bucket.
#!/bin/bash
# deploy to s3
function deploy(){
aws s3 rm s3://bucketname --profile Administrator --recursive
aws s3 sync ./ s3://bucketname --profile Administrator --exclude='node_modules/*' --exclude='.git/*' --exclude='clickCounter.py' --exclude='package-lock.json' --exclude='bundle.js.map' --exclude='package.json' --exclude='webpack_dev_server.js' --exclude='.vscode/*' --exclude='.DS_Store'
}
deploy
但是-如您所见,我有很多文件要排除在外,并且此列表将来可能会增加.
However - as you can see, I have quite a few files to be excluded, and this list may increase in the future.
所以我的问题是:有什么办法可以将要排除的文件放入数组中,然后对其进行迭代?
So my question: Is there a way I can just put the files to be excluded into an array and then iterate over that?
也许是这样的:
#!/bin/bash
arrayOfExcludedItems = (node_modules/* package-lock.json bundle.js.map )
# deploy to s3
function deploy(){
aws s3 rm s3://bucketname --profile Administrator --recursive
aws s3 sync ./ s3://bucketname --profile Administrator for item in arrayOfExcludedItems --exclude
}
deploy
推荐答案
您不能在参数列表的中间使用循环,但是可以进行文本替换,在参数的开头添加"--exclude =每个元素.但是,首先,您必须正确地声明该数组;这意味着 =
周围没有空格,并且您需要引用任何包含通配符的条目(不想当场扩展):
You can't use a loop in the middle of an argument list, but you can do a textual substitution adding "--exclude=" to the beginning of each element. First, though, you must declare the array correctly; that means no spaces around the =
, and you need to quote any entries that contain wildcards (that you don't want expanded on the spot):
arrayOfExcludedItems=('node_modules/*' '.git/*' clickCounter.py package-lock.json \
bundle.js.map package.json webpack_dev_server.js '.vscode/*' .DS_Store)
然后使用如下数组:
aws s3 sync ./ s3://bucketname --profile Administrator \
"${arrayOfExcludedItems[@]/#/--exclude=}"
工作原理: [@]
告诉外壳程序获取数组的所有元素(将每个元素作为一个单独的单词进行处理),即/#a/b
part告诉它在每个元素的开头用 b
替换 a
,但是 a
为空,因此实际上只添加了"--exclude ="一开始.哦,它周围的双引号告诉外壳程序不要扩展通配符或以其他方式弄乱结果.
How it works: the [@]
tells the shell to get all elements of the array (treating each as a separate word), the /#a/b
part tells it to replace a
at the beginning of each element with b
, but a
is empty so it effectively just adds "--exclude=" to the beginning. Oh, and the double-quotes around it tells the shell not to expand wildcards or otherwise mess with the results.
这篇关于AWS CLI S3同步:如何排除多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!