I am trying to run a aws command to update the cache control metadata for one file:
aws s3 cp s3://mybucket/file.js s3://mybucket/file.js --region us-east-1 --acl public-read --metadata-directive REPLACE --cache-control "public, max-age=86400"
However, I want to run this command against multiple selected files. So I went ahead to make this command reusable:
bucket="s3://mybucket"
region="us-east-1"
updateflag="--region $region --acl public-read --metadata-directive REPLACE --cache-control \"public, max-age=86400\""
aws s3 cp $bucket/fileA.js $bucket/fileB.js $updateflag
But this does not work ! It gives Unknown options: max-age=86400"
error.
I've tried a few ways around double quotation marks but the only time it works is like this:
updateflag="--region $region --acl public-read --metadata-directive REPLACE"
cacheflag="public, max-age=86400"
aws s3 cp $bucket/em.js $bucket/em.js $updateflag --cache-control "$cacheflag"
What went wrong when I get all options in one variable only ?
UPDATE:
Thanks for pointing out duplicated questions.
I found this question answers my question: Why does shell ignore quotes in arguments passed to it through variables?
I ended up doing the following:
set_cache_control () {
updateflag=(--region $region --acl public-read --metadata-directive REPLACE --cache-control '"public, max-age=86400"')
aws s3 cp $bucket/$1 $bucket/$1 "${updateflag[@]}"
}
set_cache_control fileA.js
set_cache_control fileB.js
```