Search code examples
bashamazon-s3s3cmd

bash script to ittarate trough files and push to s3


i have the following bash script:

s3upload() {
    n=$1
    extension=`file $n | cut -d ' ' -f2 | awk '{print tolower($0)}'` 
    mimetype=`file --mime-type $n | cut -d ' ' -f2`
    fullpath=`readlink -f $n`
    expires="Expires:`date -u +"%a, %d %b %Y %H:%M:%S GMT" --date "+1 years"`" 
    cache='Cache-Control:max-age=1296000, public'
    s3upload="s3cmd put -vvv --recursive setacl --acl-public --add-header $expires --add-header $cache -m $mimetype $fullpath s3://ccc-public/catalog/"  

    response=`$s3upload`
    echo $response
}
export -f s3upload
find nas/cdn/catalog/drawings/ \( ! -regex '.*/\..*' \) -type f | s3upload

when i execute it the file names are not returned!

i like for the script to check each file mime-type and set the expiry and cache.

if i run find nas/cdn/catalog/drawings/ \( ! -regex '.*/\..*' \) -type f this works and returns:

nas/cdn/catalog/drawings/N16.jpg
nas/cdn/catalog/drawings/EPB01.jpg
nas/cdn/catalog/drawings/N80.jpg
nas/cdn/catalog/drawings/EP22.jpg
nas/cdn/catalog/drawings/N95.jpg

any advice much appreciated.


Solution

  • Try doing it this way:

    IFS=; while read -r file; do
      s3upload "$file"
    done <<< $(find nas/cdn/catalog/drawings/ \( ! -regex '.*/\..*' \) -type f)