bash脚本迭代文件并推送到s3

时间:2014-12-10 13:01:00

标签: bash amazon-s3 s3cmd

我有以下bash脚本:

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

当我执行它时,不会返回文件名!

我喜欢脚本检查每个文件的mime-type并设置expiry和cache。

如果我运行find nas/cdn/catalog/drawings/ \( ! -regex '.*/\..*' \) -type f,则可以运行并返回:

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

任何建议都非常感激。

2 个答案:

答案 0 :(得分:0)

好吧,我想通了

expires="Expires:`date -u +"%a, %d %b %Y %H:%M:%S GMT" --date "+6 months"`"
cache='Cache-Control:max-age=1296000, public'

for f in $(find nas/cdn/catalog/drawings/ \( ! -regex '.*/\..*' \) -type f)
do
    extension=`file $f | cut -d ' ' -f2 | awk '{print tolower($0)}'`
    mimetype=`file --mime-type $f | cut -d ' ' -f2`
    fullpath=`readlink -f $f`
    echo $expires
    #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
done

答案 1 :(得分:0)

尝试这样做:

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