7f2282a7 |
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" |
436baa0c |
28 | printf "Usage %s: [-vS] [-H file] [-a file] [-b speed] -k key -s file -T file url\n" "$weAreKnownAs" |
7f2282a7 |
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 " -T\tfile\tmandatory\tFile (or stdin with -) to PUT\n" |
35 | printf " -S\t\toptional\tUse https\n" |
36 | printf " -H\tfile\toptional\tFile to write response headers to\n" |
37 | printf " -a\tfile\toptional\tFile to read Amazon custom headers from (X-Amz-Date is not allowed)\n" |
436baa0c |
38 | printf " -b\tspeed\toptional\tBandwidth limit in units/sec\n" |
7f2282a7 |
39 | printf " -c\tMIME\toptional\tMIME Content type. Default is text/plain\n" |
40 | printf " \turl\tmandatory\trelative url including bucket name and leading slash, eg /bucket/path/to/object?acl. Assumed to be already encoded\n" |
41 | printf "\n" |
42 | printf "Notes\n" |
43 | printf "Specify proxies using a ~/.curlrc file\n" |
44 | printf "Specify content to PUT using stdin using option -T -\n" |
45 | exit $exitCode |
46 | } |
47 | |
48 | function parseOptions |
49 | { |
50 | verbose="" |
51 | url="" |
52 | awsAccessKeyId="" |
53 | awsAccessSecretKeyIdFile="" |
54 | protocol="http" |
55 | fileToUpload="" |
56 | dumpHeaderFile="/dev/null" |
57 | amazonHeaderFile="/dev/null" |
436baa0c |
58 | bwlimit="" |
7f2282a7 |
59 | contentType="text/plain" |
60 | while getopts "hvk:s:SH:T:a:c:" optionName; do |
61 | case "$optionName" in |
62 | h) printHelpAndExit 0;; |
63 | v) verbose="-v";; |
64 | k) awsAccessKeyId="$OPTARG";; |
65 | s) awsAccessSecretKeyIdFile="$OPTARG" |
66 | if [ ! -e "$awsAccessSecretKeyIdFile" ]; then |
67 | printErrorHelpAndExit "AWS Secret Key Id file does not exist" $userSpecifiedDataErrorExitCode |
68 | fi;; |
69 | S) protocol="https";; |
70 | H) dumpHeaderFile="$OPTARG";; |
71 | T) fileToUpload="$OPTARG";; |
72 | a) amazonHeaderFile="$OPTARG";; |
436baa0c |
73 | b) bwlimit="--limit-rate $OPTARG";; |
7f2282a7 |
74 | c) contentType="$OPTARG";; |
75 | [?]) printErrorHelpAndExit "Option not recognised" $userSpecifiedDataErrorExitCode;; |
76 | esac |
77 | done |
78 | if [ 1 -eq $OPTIND ]; then |
79 | printErrorHelpAndExit "Internal Error: parseOptions or a parent method in the call stack was not called with $"@"." $internalErrorExitCode |
80 | fi |
81 | let "toShift = $OPTIND - 1" |
82 | shift $toShift |
83 | if [ $# -eq 0 ]; then |
84 | printErrorHelpAndExit "URL not specified" $userSpecifiedDataErrorExitCode |
85 | fi |
86 | url="$1" |
87 | verifyUrl |
88 | |
89 | if [ -z "$awsAccessSecretKeyIdFile" ]; then |
90 | printErrorHelpAndExit "AWS Secret Access Key file not specified" $userSpecifiedDataErrorExitCode |
91 | elif [ -z "$awsAccessKeyId" ]; then |
92 | printErrorHelpAndExit "AWS Access Key Id not specified" $userSpecifiedDataErrorExitCode |
93 | elif [ -z "$fileToUpload" ]; then |
94 | printErrorHelpAndExit "File to upload not specified" $userSpecifiedDataErrorExitCode |
95 | fi |
96 | } |
97 | |
98 | function prepareToRunCurl |
99 | { |
100 | readonly verb="PUT" |
101 | if [ ! "-" = "$fileToUpload" ]; then |
102 | readonly contentMD5="$(base64EncodedMD5 "$fileToUpload")" |
436baa0c |
103 | readonly verbToPass="-T \"$fileToUpload\" $bwlimit" |
7f2282a7 |
104 | else |
105 | readonly contentMD5="" |
436baa0c |
106 | readonly verbToPass="-T - $bwlimit" |
7f2282a7 |
107 | fi |
108 | } |
109 | |
110 | readonly weAreKnownAs="$(basename $0)" |
111 | readonly ourPath="$(dirname $0)" |
112 | |
113 | readonly commonFunctions="$ourPath/s3-common-functions" |
114 | if [ -e "$commonFunctions" ]; then |
115 | source "$commonFunctions" |
116 | else |
117 | version="Unknown" |
118 | invalidEnvironmentExitCode=4 |
119 | printErrorHelpAndExit "$weAreKnownAs: Could not locate file s3-common-functions" $invalidEnvironmentExitCode |
120 | fi |
121 | |
122 | main "$@" |