| 1 | #! /usr/bin/env bash |
| 2 | cat > /dev/null << EndOfLicence |
| 3 | s3-bash |
| 4 | Copyright 2007 Raphael James Cohn |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 7 | in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software distributed under the License |
| 13 | is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 14 | or implied. See the License for the specific language governing permissions and limitations under |
| 15 | the License. |
| 16 | EndOfLicence |
| 17 | |
| 18 | # Pragmas |
| 19 | set -u |
| 20 | set -e |
| 21 | |
| 22 | function printHelpAndExit |
| 23 | { |
| 24 | exitCode=$1 |
| 25 | printf "%s: version %s\n" "$weAreKnownAs" "$version" |
| 26 | printf "Part of s3-bash. Latest version is at %s\n" 'http://code.google.com/p/s3-bash/' |
| 27 | printf "Usage %s: -h\n" "$weAreKnownAs" |
| 28 | printf "Usage %s: [-vS] [-H file] [-a file] -k key -s file url\n" "$weAreKnownAs" |
| 29 | printf " Option\tType\tRequirement\tDescription\n" |
| 30 | printf " -h\t\tprecedent\tprint this help\n" |
| 31 | printf " -v\t\toptional\tverbose output\n" |
| 32 | printf " -k\tstring\tmandatory\tAWS Access Key Id\n" |
| 33 | printf " -s\tfile\tmandatory\tAWS Secret Access Key Id File\n" |
| 34 | printf " -S\t\toptional\tUse https\n" |
| 35 | printf " -H\tfile\toptional\tFile to write response headers to\n" |
| 36 | printf " -a\tfile\toptional\tFile to read Amazon custom headers from (X-Amz-Date is not allowed)\n" |
| 37 | printf " \turl\tmandatory\trelative url including bucket name and leading slash, eg /bucket/path/to/object?acl. Assumed to be already encoded\n" |
| 38 | printf "\n" |
| 39 | printf "Notes\n" |
| 40 | printf "Specify proxies using a ~/.curlrc file\n" |
| 41 | exit $exitCode |
| 42 | } |
| 43 | |
| 44 | function parseOptions |
| 45 | { |
| 46 | verbose="" |
| 47 | url="" |
| 48 | awsAccessKeyId="" |
| 49 | awsAccessSecretKeyIdFile="" |
| 50 | protocol="http" |
| 51 | dumpHeaderFile="/dev/null" |
| 52 | amazonHeaderFile="/dev/null" |
| 53 | while getopts "hvk:s:SH:T:a:" optionName; do |
| 54 | case "$optionName" in |
| 55 | h) printHelpAndExit 0;; |
| 56 | v) verbose="-v";; |
| 57 | k) awsAccessKeyId="$OPTARG";; |
| 58 | s) awsAccessSecretKeyIdFile="$OPTARG" |
| 59 | if [ ! -e "$awsAccessSecretKeyIdFile" ]; then |
| 60 | printErrorHelpAndExit "AWS Secret Key Id file does not exist" $userSpecifiedDataErrorExitCode |
| 61 | fi;; |
| 62 | S) protocol="https";; |
| 63 | H) dumpHeaderFile="$OPTARG";; |
| 64 | a) amazonHeaderFile="$OPTARG";; |
| 65 | [?]) printErrorHelpAndExit "Option not recognised" $userSpecifiedDataErrorExitCode;; |
| 66 | esac |
| 67 | done |
| 68 | if [ 1 -eq $OPTIND ]; then |
| 69 | printErrorHelpAndExit "Internal Error: parseOptions or a parent method in the call stack was not called with $"@"." $internalErrorExitCode |
| 70 | fi |
| 71 | let "toShift = $OPTIND - 1" |
| 72 | shift $toShift |
| 73 | if [ $# -eq 0 ]; then |
| 74 | printErrorHelpAndExit "URL not specified" $userSpecifiedDataErrorExitCode |
| 75 | fi |
| 76 | url="$1" |
| 77 | verifyUrl |
| 78 | |
| 79 | if [ -z "$awsAccessSecretKeyIdFile" ]; then |
| 80 | printErrorHelpAndExit "AWS Secret Access Key file not specified" $userSpecifiedDataErrorExitCode |
| 81 | elif [ -z "$awsAccessKeyId" ]; then |
| 82 | printErrorHelpAndExit "AWS Access Key Id not specified" $userSpecifiedDataErrorExitCode |
| 83 | fi |
| 84 | } |
| 85 | |
| 86 | function prepareToRunCurl |
| 87 | { |
| 88 | readonly verb="DELETE" |
| 89 | readonly verbToPass="-X DELETE" |
| 90 | readonly contentMD5="" |
| 91 | readonly contentType="" |
| 92 | } |
| 93 | |
| 94 | readonly weAreKnownAs="$(basename $0)" |
| 95 | readonly ourPath="$(dirname $0)" |
| 96 | |
| 97 | readonly commonFunctions="$ourPath/s3-common-functions" |
| 98 | if [ -e "$commonFunctions" ]; then |
| 99 | source "$commonFunctions" |
| 100 | else |
| 101 | version="Unknown" |
| 102 | invalidEnvironmentExitCode=4 |
| 103 | printHelpAndExit "$weAreKnownAs: Could not locate file s3-common-functions" $invalidEnvironmentExitCode |
| 104 | fi |
| 105 | |
| 106 | main "$@" |