Initial check-in of Amazon S3 helper scripts.
[hcoop/scripts.git] / s3-get
CommitLineData
7f2282a7 1#! /usr/bin/env bash
2cat > /dev/null << EndOfLicence
3s3-bash
4Copyright 2007 Raphael James Cohn
5
6Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software distributed under the License
13is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14or implied. See the License for the specific language governing permissions and limitations under
15the License.
16EndOfLicence
17
18# Pragmas
19set -u
20set -e
21
22function 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 printf "Content is returned on stdout\n"
42 exit $exitCode
43}
44
45function parseOptions
46{
47 verbose=""
48 url=""
49 awsAccessKeyId=""
50 awsAccessSecretKeyIdFile=""
51 protocol="http"
52 dumpHeaderFile="/dev/null"
53 amazonHeaderFile="/dev/null"
54 while getopts "hvk:s:SH:a:" optionName; do
55 case "$optionName" in
56 h) printHelpAndExit 0;;
57 v) verbose="-v";;
58 k) awsAccessKeyId="$OPTARG";;
59 s) awsAccessSecretKeyIdFile="$OPTARG"
60 if [ ! -e "$awsAccessSecretKeyIdFile" ]; then
61 printErrorHelpAndExit "AWS Secret Key Id file does not exist" $userSpecifiedDataErrorExitCode
62 fi;;
63 S) protocol="https";;
64 H) dumpHeaderFile="$OPTARG";;
65 a) amazonHeaderFile="$OPTARG"
66 ;;
67 [?]) printErrorHelpAndExit "Option not recognised" $userSpecifiedDataErrorExitCode;;
68 esac
69 done
70 if [ 1 -eq $OPTIND ]; then
71 printErrorHelpAndExit "Internal Error: parseOptions or a parent method in the call stack was not called with $"@"." $internalErrorExitCode
72 fi
73 let "toShift = $OPTIND - 1"
74 shift $toShift
75 if [ $# -eq 0 ]; then
76 printErrorHelpAndExit "URL not specified" $userSpecifiedDataErrorExitCode
77 fi
78 url="$1"
79 verifyUrl
80
81 if [ -z "$awsAccessSecretKeyIdFile" ]; then
82 printErrorHelpAndExit "AWS Secret Access Key file not specified" $userSpecifiedDataErrorExitCode
83 elif [ -z "$awsAccessKeyId" ]; then
84 printErrorHelpAndExit "AWS Access Key Id not specified" $userSpecifiedDataErrorExitCode
85 fi
86}
87
88function prepareToRunCurl
89{
90 readonly verb="GET"
91 readonly verbToPass="--get"
92 readonly contentMD5=""
93 readonly contentType=""
94}
95
96readonly weAreKnownAs="$(basename $0)"
97readonly ourPath="$(dirname $0)"
98
99readonly commonFunctions="$ourPath/s3-common-functions"
100if [ -e "$commonFunctions" ]; then
101 source "$commonFunctions"
102else
103 version="Unknown"
104 invalidEnvironmentExitCode=4
105 printErrorHelpAndExit "$weAreKnownAs: Could not locate file s3-common-functions" $invalidEnvironmentExitCode
106fi
107
108main "$@"