-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload-spaces.sh
executable file
·55 lines (46 loc) · 1.7 KB
/
upload-spaces.sh
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
#!/usr/bin/env bash
MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
for line in $(cat "${MY_DIR}/.env"); do export $line; done
bucket=${1}
shift
if [ -f ${bucket} ]; then
echo "Usage: ${__FILE__} <bucket_name> [files]"
echo "<bucket_name>:\tBucket that will receive the files"
echo "[files]:\tAll the files to upload with their desired result path"
exit 1
fi
dateValue=$(TZ=utc date -R)
ENDPOINT="nyc3.digitaloceanspaces.com"
# This doesn't work on OSX because collate usage is outdated. Leaving in since using linux for real uploads.
urlencode() {
# Usage: urlencode "string"
old_lc_collate="${LC_COLLATE}"
LC_COLLATE='C'
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
' ') printf "%%20" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE="${old_lc_collate}"
}
for file in $*; do
filename=$(basename "${file}")
dirname=$(dirname "${file}")
resource="/${bucket}/${dirname}/$(urlencode ${filename})"
contentType=$(file --mime "${file}" | sed -e 's/^.*: //g')
acl="public-read"
stringToSign="PUT\n\n${contentType}\n${dateValue}\nx-amz-acl:${acl}\n${resource}"
echo ${stringToSign}
signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${DO_SPACES_SECRET_ACCESS_KEY} -binary | base64)
curl -X PUT -T "${file}" \
-H "Host: ${bucket}.${ENDPOINT}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${DO_SPACES_ACCESS_KEY_ID}:${signature}" \
-H "x-amz-acl: ${acl}" \
"https://${bucket}.${ENDPOINT}/${dirname}/$(urlencode ${filename})" && echo "Uploaded ${file}"
done