aws cli s3 sync: how to exclude multiple files

為{幸葍}努か 提交于 2021-02-10 18:48:55

问题


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?

Perhaps something like:

#!/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

回答1:


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)

Then use the array like this:

aws s3 sync ./ s3://bucketname --profile Administrator \
  "${arrayOfExcludedItems[@]/#/--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.



来源:https://stackoverflow.com/questions/55045423/aws-cli-s3-sync-how-to-exclude-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!