Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cli s3 sync: how to exclude multiple files

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
like image 330
Filtenborg Avatar asked Oct 15 '25 07:10

Filtenborg


1 Answers

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.

like image 105
Gordon Davisson Avatar answered Oct 18 '25 04:10

Gordon Davisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!