X-Git-Url: https://git.hcoop.net/hcoop/scripts.git/blobdiff_plain/a0d92e6305b3c637ec6be3b6630ebe71eb2c0348..7cc8af5787fcfc12705dfcf541eeea10e325f7dd:/s3 diff --git a/s3 b/s3 index 4d070b4..9ca45f8 100755 --- a/s3 +++ b/s3 @@ -3,7 +3,9 @@ # Licensed under the terms of the GNU GPL v2 # Copyright 2007 Victor Lowther - +CURL=/home/mwolson_admin/bin/curl +HMAC=$(dirname $0)/s3-hmac +ATTEMPTS=3 # print a message and bail die() { @@ -39,8 +41,15 @@ check_dep() { (( res == 0 )) || die "aborting." } +check_hmac() { + if test ! -f $HMAC || test ! -x $HMAC; then + die "hmac script not found or not executable." + fi +} + check_deps() { - check_dep openssl date hmac cat grep curl + check_dep openssl date cat grep + check_hmac check_s3 } @@ -96,7 +105,7 @@ s3_signature_string() { printf "%s\n%s\n%s\n%s\n%s%s%s" \ "${verb}" "${md5}" "${mime}" "${date}" \ "${headers}" "${bucket}" "${resource}" | \ - hmac sha1 "${S3_SECRET_ACCESS_KEY}" | openssl base64 -e -a + $HMAC sha1 "${S3_SECRET_ACCESS_KEY}" | openssl base64 -e -a } # cheesy, but it is the best way to have multiple headers. @@ -115,12 +124,14 @@ s3_curl() { # $2 = remote bucket. # $3 = remote name # $4 = local name. - local bucket remote date sig md5 arg inout headers + # $5 = bandwidth limit. + local bucket remote date sig md5 arg inout headers tries ret # header handling is kinda fugly, but it works. bucket="${2:+/${2}}/" # slashify the bucket remote="$(urlenc "${3}")" # if you don't, strange things may happen. stdopts="--connect-timeout 10 --fail --silent" [[ $CURL_S3_DEBUG == true ]] && stdopts="${stdopts} --show-error --fail" + test -n "${5}" && stdopts="${stdopts} --limit-rate ${5}" case "${1}" in GET) arg="-o" inout="${4:--}" # stdout if no $4 ;; @@ -147,16 +158,23 @@ s3_curl() { headers[${#headers[@]}]="Authorization: AWS ${S3_ACCESS_KEY_ID}:${sig}" headers[${#headers[@]}]="Date: ${date}" [[ ${md5} ]] && headers[${#headers[@]}]="Content-MD5: ${md5}" - curl ${arg} "${inout}" ${stdopts} -K <(curl_headers "${headers[@]}") \ + tries=0 + while test $tries -lt 3; do + tries=$(expr $tries + 1) + $CURL ${arg} "${inout}" ${stdopts} -K <(curl_headers "${headers[@]}") \ "http://s3.amazonaws.com${bucket}${remote}" - return $? + ret=$? + test $ret -eq 0 && break; + done + return $ret } s3_put() { # $1 = remote bucket to put it into # $2 = remote name to put # $3 = file to put. This must be present if $2 is. - s3_curl PUT "${1}" "${2}" "${3:-${2}}" + # $4 = bandwidth limit. + s3_curl PUT "${1}" "${2}" "${3:-${2}}" "${4}" return $? } @@ -165,7 +183,8 @@ s3_get() { # $2 = remote file to get # $3 = local file to get into. Will be overwritten if it exists. # If this contains a path, that path must exist before calling this. - s3_curl GET "${1}" "${2}" "${3:-${2}}" + # $4 = bandwidth limit. + s3_curl GET "${1}" "${2}" "${3:-${2}}" "${4}" return $? }