Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 102 lines (84 sloc) 2.248 kB
9fd73bc @cosmin Added initial versions of the scripts used during my Deploying Web Ap…
authored
1 #!/usr/bin/env bash
2
3 SCRIPT_NAME=$0
4
5 usage() {
6 cat <<EOF
7 Usage: $SCRIPT_NAME OPTIONS
8
9
10 Required:
11 -b BUCKET_NAME the bucket name
12 -l LOCATION the local path to upload (recursively)
13
14 Credentials:
15 -k ACCESS_KEY the AWS access key. From your Security Credentials
16 -s AWS_SECRET_KEY the AWS secret key. From your Security Credentials
17 EOF
18 }
19
20 die() {
21 message=$1
22 error_code=$2
23
24 echo "$SCRIPT_NAME: $message" 1>&2
25 usage
26 exit $error_code
27 }
28
29 while getopts "hb:l:k:s:" opt; do
30 case "$opt" in
31 h)
32 usage
33 exit 0
34 ;;
35 b)
36 export BUCKET_NAME="$OPTARG"
37 ;;
38 l)
39 export LOCATION="$OPTARG"
40 ;;
41 k)
42 export ACCESS_KEY="$OPTARG"
43 ;;
44 s)
45 SECRET_KEY="$OPTARG"
46 touch ~/.aws.secret.key
47 chmod 600 ~/.aws.secret.key
48 echo -n "$SECRET_KEY" > ~/.aws.secret.key
49 SECRET_KEY_FILE="$HOME/.aws.secret.key"
50 ;;
51 [?])
52 die "unknown option $opt" 10
53 ;;
54 esac
55 done
56
57
58 if [ -z "$BUCKET_NAME" ]; then
59 die "BUCKET_NAME is required" 1
60 fi
61
62 if [ -z "$LOCATION" ]; then
63 die "LOCATION is required" 2
64 else
65 if [ ! -d "$LOCATION" ]; then
66 die "LOCATION must point to a directory that exists" 3
67 else
68 export LOCATION_PATH=$(cd $LOCATION && pwd)
69 fi
70 fi
71
72 if [ -z "$ACCESS_KEY" ]; then
73 die "ACCESS_KEY is required" 4
74 fi
75
76 if [ -z "SECRET_KEY" ]; then
77 die "SECRET_KEY is required" 5
78 fi
79
80 extract_s3_bash() {
81 wget https://s3.amazonaws.com/cloudinitfiles/s3-bash.tgz
82 tar zxvf s3-bash.tgz
83 }
84
85 get_mime() {
86 python -c "import mimetypes; print mimetypes.guess_type(\"$1\")[0] or \"text/plain\""
87 }
88
89 upload_files() {
90 HEADER_FILE=`mktemp -t xzawsXXXXXX`
91 echo "X-Amz-Acl: public-read" > $HEADER_FILE
92 for file in `find $LOCATION_PATH -type f | sed "s|$LOCATION_PATH/||g"`; do
93 FULL_PATH="$LOCATION_PATH/$file"
94 MIME=`get_mime $FULL_PATH`
95 ./s3-bash/s3-put -a "$HEADER_FILE" -k "$ACCESS_KEY" -s "$SECRET_KEY_FILE" -T "$FULL_PATH" -c "$MIME" "/$BUCKET_NAME/$file"
96 done
97 rm $HEADER_FILE
98 }
99
100 extract_s3_bash
101 upload_files
Something went wrong with that request. Please try again.