-
Notifications
You must be signed in to change notification settings - Fork 0
/
putS3ObjectWithParameters.sh
111 lines (93 loc) · 3.58 KB
/
putS3ObjectWithParameters.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
set -e
shellScriptName=`basename $0`
USAGE="___________________________________________________________________________________________________\n
USAGE: ${shellScriptName} <options>
OPTIONS:
[-k | --key-id (AWS Access Key ID)]
[-s | --secret (AWS Secret Access Key)]
[-c | --merchant-code (Merchant Code Given by L1)]
[-f | --local-file (Full location to local file)]
[-b | --bucket (Bucket name where you put the file)]
[-t | --content-type (OPTIONAL - Content Type of the file)]
[-h | --help (OPTIONAL - print this help menu)]
EXAMPLES:
./${shellScriptName} --merchant-code folder1 --key-id ABCKEYID --secret ABCSECRETACCESSKEY123 --local-file ./fileName.png --bucket bucket-Name-On-S3
./${shellScriptName} -c folder1 -k ABCKEYID -s ABCSECRETACCESSKEY123 -f /Users/ksalera/temp/fileName.png -b bucket-Name-On-S3
Override default content-type
./${shellScriptName} -c folder1 -k ABCKEYID -s ABCSECRETACCESSKEY123 -f ./fileName.png -t image/png -b bucket-Name-On-S3
___________________________________________________________________________________________________
"
if [ $# -eq 0 ]; then
echo "No arguments provided"
echo "${USAGE}"
exit 1
fi
while [ "$1" != "" ]; do
case "$1" in
"-k" | "--key-id")
shift
AWSAccessKeyId=$1
;;
"-s" | "--secret")
shift
YourSecretAccessKeyID=$1
;;
"-c" | "--merchant-code")
shift
MerchantCode=$1
;;
"-f" | "--local-file")
shift
LocalFilePath=$1
LocalFileName=`basename $LocalFilePath`
;;
"-b" | "--bucket")
shift
Bucket=$1
;;
"-t" | "--content-type")
shift
ContentType=$1
;;
"-h" | "--help")
echo "${USAGE}"
exit 0
;;
*)
echo "${USAGE}"
exit 1
;;
esac
shift
done
if [ -z $AWSAccessKeyId ] || [ -z $YourSecretAccessKeyID ] || [ -z $MerchantCode ] || [ -z $LocalFilePath ] || [ -z $LocalFileName ] || [ -z $Bucket ]; then
echo "All parameters not passed"
echo "${USAGE}"
exit 1
fi
if [ -z $Bucket ]; then Bucket="bucket-default-test1"; fi
if [ -z $ContentType ]; then ContentType="text/plain"; fi
ContentLength=$(wc -c ${LocalFilePath} | awk '{print $1}')
echo "${LocalFilePath} of type ${ContentType} is of ${ContentLength} bytes"
# https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationConstructingCanonicalizedAmzHeaders
CanonicalizedAmzHeaders="x-amz-server-side-encryption:AES256\n"
echo "CanonicalizedAmzHeaders is: ${CanonicalizedAmzHeaders}"
# https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheCanonicalizedResourceElement
CanonicalizedResource="/${Bucket}/${MerchantCode}/${LocalFileName}"
echo "CanonicalizedResource is: ${CanonicalizedResource}"
Date="`date +'%a, %d %b %Y %H:%M:%S %z'`"
echo "Date is: ${Date}"
StringToSign="PUT\n\n${ContentType}\n${Date}\n${CanonicalizedAmzHeaders}${CanonicalizedResource}"
echo "StringToSign is: ${StringToSign}"
Signature=`echo -en "${StringToSign}" | openssl sha1 -hmac ${YourSecretAccessKeyID} -binary | base64`
echo "Signature is: ${Signature}"
Authorization="AWS ${AWSAccessKeyId}:${Signature}"
echo "Authorization is: ${Authorization}"
curl -X PUT -T "${LocalFilePath}" \
-H "Date: ${Date}" \
-H "Content-Type: ${ContentType}" \
-H "Content-Length: ${ContentLength}" \
-H "X-Amz-Server-Side-Encryption: AES256" \
-H "Authorization: ${Authorization}" \
"https://s3.amazonaws.com${CanonicalizedResource}"