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