· 8 years ago · Jan 13, 2018, 10:40 PM
1#!/usr/bin/env bash
2
3function upload {
4 local file=$1
5 eval file=${file}
6 local s3=$2
7 local accessKey=$3
8 local secretKey=$4
9 local dateTime=$(echo $5 | xargs)
10 local verbose=$6
11 local dryrun=$7
12
13 local s3Path=${s3#s3://}
14 local bucket=${s3Path%%/*}
15 local path=${s3Path#*/}
16
17 if [[ "${s3}" == "${s3Path}" ]]; then
18 echo "Error: destination ${s3} is not correct"
19 echo "An s3 url looks like this: s3://<bucket>/<object>"
20 exit 1
21 fi
22
23 if [[ "${dateTime}" == "" ]]; then
24 dateTime=$(date -u +"%Y%m%dT%H%M%SZ")
25 fi
26
27 local size=$(stat -f "%z" ${file})
28
29 local url=https://${bucket}.s3.amazonaws.com/${path}
30 declare -a headers=("Content-Length:${size}")
31
32 verboseTmp=''
33 if [[ "${verbose}" != "" ]]; then
34 verboseTmp="/tmp/$$.put.tmp"
35 fi
36
37 local signature=$(createSignature PUT "${url}" "${accessKey}" "${secretKey}" ${dateTime} 3600 's3' 'us-east-1' 1 "${verboseTmp}" headers[@])
38
39 if [[ "${verbose}" != "" ]]; then
40 cat ${verboseTmp} > ${verbose}
41 cat - <<IntermediateValuese > ${verbose}
42
43File: ${file}
44S3: ${s3}
45Prt: ${s3Path}
46Bucket: ${bucket}
47Path: ${path}
48
49dateTime: ${dateTime}
50Size: ${size}
51
52URL: ${url}
53signature: ${signature}
54
55
56IntermediateValuese
57 local id=$$
58 if [[ ${dryrun} == 1 ]]; then
59 id=dryrun
60 fi
61 local response="/tmp/${id}.response"
62 echo >> ${verbose}
63 echo "UPLOAD" >> ${verbose}
64 echo "======" >> ${verbose}
65 echo "curl -o ${response} --dump-header ${response}.header --header "${signature}" --header "Content-Length: ${size}" --header "X-Amz-Content-SHA256: UNSIGNED-PAYLOAD" --header "X-Amz-Date: ${dateTime}" --data-binary "@${file}" -X PUT ${url}" >> ${verbose}
66 fi
67
68 if [[ ${dryrun} == 0 ]]; then
69 curl -o ${response} --dump-header ${response}.header --header "${signature}" --header "Content-Length: ${size}" --header "X-Amz-Content-SHA256: UNSIGNED-PAYLOAD" --header "X-Amz-Date: ${dateTime}" --data-binary "@${file}" -X PUT ${url}
70 if [[ "${verbose}" != "" ]]; then
71 echo "HEADER" >> ${verbose}
72 echo "======" >> ${verbose}
73 cat ${respose}.header >> ${verbose}
74 echo "BODY" >> ${verbose}
75 echo "====" >> ${verbose}
76 cat ${respose} >> ${verbose}
77 fi
78 fi
79}
80
81#!/usr/bin/env bash
82
83function download {
84 local s3=$1
85 local file=$2
86 eval file=${file}
87 local accessKey=$3
88 local secretKey=$4
89 local dateTime=$(echo $5 | xargs)
90 local verbose=$6
91 local dryrun=$7
92
93 local s3Path=${s3#s3://}
94 local bucket=${s3Path%%/*}
95 local path=${s3Path#*/}
96
97 if [[ "${s3}" == "${s3Path}" ]]; then
98 echo "Error: destination ${s3} is not correct"
99 echo "An s3 url looks like this: s3://<bucket>/<object>"
100 exit 1
101 fi
102
103 if [[ "${dateTime}" == "" ]]; then
104 dateTime=$(date -u +"%Y%m%dT%H%M%SZ")
105 fi
106
107 local url=https://s3.amazonaws.com/${bucket}/${path}
108 declare -a headers
109
110 verboseTmp=''
111 if [[ "${verbose}" != "" ]]; then
112 verboseTmp="/tmp/$$.get.tmp"
113 fi
114
115 local signature=$(createSignature GET "${url}" "${accessKey}" "${secretKey}" ${dateTime} 3600 's3' 'us-east-1' 1 "${verboseTmp}" headers[@])
116
117 if [[ "${verbose}" != "" ]]; then
118 cat ${verboseTmp} > ${verbose}
119 cat - <<IntermediateValuese > ${verbose}
120
121S3: ${s3}
122File: ${file}
123Prt: ${s3Path}
124Bucket: ${bucket}
125Path: ${path}
126
127dateTime: ${dateTime}
128
129URL: ${url}
130signature: ${signature}
131
132IntermediateValuese
133 local id=$$
134 if [[ ${dryrun} == 1 ]]; then
135 id=dryrun
136 fi
137 local response="/tmp/${id}.response"
138 echo >> ${verbose}
139 echo "DOWNLOAD" >> ${verbose}
140 echo "========" >> ${verbose}
141 echo "curl -o "${file}" --dump-header ${response}.header --header "${signature}" --header "X-Amz-Date: ${dateTime}" --header "X-Amz-Content-SHA256: UNSIGNED-PAYLOAD" ${url}" >> ${verbose}
142 fi
143
144 if [[ ${dryrun} == 0 ]]; then
145 curl -o ${file} --dump-header ${response}.header --header "${signature}" --header "X-Amz-Date: ${dateTime}" --header "X-Amz-Content-SHA256: UNSIGNED-PAYLOAD" ${url}
146 if [[ "${verbose}" != "" ]]; then
147 echo "HEADER" >> ${verbose}
148 echo "======" >> ${verbose}
149 cat ${response}.header >> ${verbose}
150 fi
151 fi
152}