-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.sh
176 lines (121 loc) Β· 3.94 KB
/
upload.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
set -e
configFile=$1
echo "Welcome to my simple cloud upload tool π, I hope you finding it very useful. π Gracias!!!"
aws_storage_service=("aurora" "dynamodb" "rds" "s3")
isAuth() {
echo "Checking if user is authenticated...."
USER_ID=$(aws sts get-caller-identity | jq '.UserId')
if [[ -n "$USER_ID" ]]; then
echo "User Id is set...."
else
echo "User Id not set...π"
exit 1
fi
}
isAccess() {
echo "Checking Access...."
AccessKey=$(aws iam list-access-keys | jq '.AccessKeyMetadata[].AccessKeyId')
UserName=$(aws iam list-access-keys | jq '.AccessKeyMetadata[].UserName')
status=$(aws iam list-access-keys | jq '.AccessKeyMetadata[].Status')
if [ -n "${AccessKey}" ] || [ -n "${UserName}" ]; then
echo "User ${UserName} is ${status}...."
else
echo "AccessKeyId not found....π"
exit 1
fi
}
processConfig() {
if [[ -z ${configFile} ]]; then
echo "Config file not found"
exit 1
else
service=$(yq '.config.service' ${configFile})
if [ -z ${service} ]; then
echo "Service field is empty. Check config file"
exit 1
else
if [[ "${aws_storage_service[@]}" =~ "${service}" ]]; then
echo "This tool supports upload to ${service}"
upload $service
else
echo "This tool doesn't support upload operations for this AWS ${service}"
exit 1
fi
fi
fi
}
upload() {
local service=$1
if [[ $service == "s3" ]]; then
echo "Uploading data to aws ${service} storage...."
uploadToS3
else
echo "Cannot upload to any storage service at the moment";
exit 1
fi
}
############################## Upload to S3 #######################################################
uploadToS3() {
isMultipleFiles=$(yq '.config.isMultipleFiles' ${configFile})
localFilePath=$(yq '.config.localFilePath' ${configFile})
bucket=$(yq '.config.bucket' ${configFile})
localFileName=$(yq '.config.fileName' ${configFile})
if [ ${isMultipleFiles} == true ]; then
echo "uploading folders...."
UploadMultipleFilesToS3 ${localFilePath} ${bucket}
else
echo "Uploading Single file...."
UploadSingleFile ${localFilePath} ${localFileName} ${bucket}
fi
}
UploadMultipleFilesToS3() {
local localFilePath=$1
local bucket=$2
if [ -z ${localFilePath} ] || [ -z ${bucket} ]; then
echo "Error: Config fields required to upload multiple files is empty. Please populate values"
exit 1
else
echo "**** uploading to S3 bucket - ${bucket} ****"
result=$(aws s3 sync ${localFilePath} s3://${bucket} )
echo "${result}"
statusCode=$?
if [[ "$statusCode" -eq 0 ]]; then
echo " Multiple file uploaded Successfully π"
exit 0
else
echo "${result}"
echo "Error: Multiple File upload not Successful...."
exit 1
fi
fi
}
UploadSingleFile() {
local localFilePath=$1
local localFileName=$2
local bucket=$3
if [ -z ${localFilePath} ] || [ -z ${bucket} ]; then
echo "Error: Config fields required to upload single file is empty. Please populate values"
exit 1
else
result=$(aws s3 cp ${localFilePath} s3://${bucket})
statusCode=$?
if [[ "$statusCode" -eq 0 ]]; then
echo "${result}"
echo " file uploaded Successfully π"
exit 0
else
echo "${result}"
echo "Error: Single File upload not Successful...."
exit 1
fi
fi
}
############################## Upload to DynaMoDB #######################################################
main() {
isAuth
isAccess
processConfig
upload
}
main