-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload
58 lines (42 loc) · 1.52 KB
/
upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Treat uninitialized vars as a reason to exit, rather than as null
set -u
# Exit on the first non-zero return code
set -e
# Exit on the first non-zero return code of any program in a pipeline
set -o pipefail
MYDIR=$( dirname "$(readlink -f "$0")" )
function die { echo "$1"; exit 1 ; }
[ -v SECRETACCESSKEY ] || die "Undefined: SECRETACCESSKEY";
[ -v ACCESSKEYID ] || die "Undefined: ACCESSKEYID";
S3SECRET="${SECRETACCESSKEY}"
S3KEY="${ACCESSKEYID}"
function putS3
{
source_path=$1
aws_path=$2
BUCKET='drunner'
echo "$source_path"
date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
md5="$(openssl md5 -binary < "${source_path}" | base64)"
content_type="$(file -b --mime-type "${source_path}")"
[[ ! "${source_path##*.}" == "css" ]] || content_type="text/css"
echo " $content_type"
string="PUT\n\n${content_type}\n${date}\n/${BUCKET}${aws_path}"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
curl -X PUT -T "${source_path}" \
-H "Host: ${BUCKET}.s3.amazonaws.com" \
-H "Date: ${date}" \
-H "Content-Type: ${content_type}" \
-H "Authorization: AWS ${S3KEY}:${signature}" \
"https://drunner.s3-ap-southeast-2.amazonaws.com${aws_path}"
# "https://${BUCKET}.s3.amazonaws.com${aws_path}"
echo " Uploaded ${source_path} to S3."
}
function main {
find www -type f -print0 | while IFS= read -r -d '' i; do
FILE="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0],"^A-Za-z0-9\-\._~\/");' "$i")"
putS3 "$FILE" "/$FILE"
done
}
main "$@"