forked from bitrise-steplib/steps-amazon-s3-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.sh
executable file
·166 lines (137 loc) · 4.3 KB
/
step.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
#!/bin/bash
THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
set -e
#=======================================
# Functions
#=======================================
RESTORE='\033[0m'
RED='\033[00;31m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
GREEN='\033[00;32m'
function color_echo {
color=$1
msg=$2
echo -e "${color}${msg}${RESTORE}"
}
function echo_fail {
msg=$1
echo
color_echo "${RED}" "${msg}"
exit 1
}
function echo_warn {
msg=$1
color_echo "${YELLOW}" "${msg}"
}
function echo_info {
msg=$1
echo
color_echo "${BLUE}" "${msg}"
}
function echo_details {
msg=$1
echo " ${msg}"
}
function echo_done {
msg=$1
color_echo "${GREEN}" " ${msg}"
}
function validate_required_input {
key=$1
value=$2
if [ -z "${value}" ] ; then
echo_fail "[!] Missing required input: ${key}"
fi
}
function validate_required_input_with_options {
key=$1
value=$2
options=$3
validate_required_input "${key}" "${value}"
found="0"
for option in "${options[@]}" ; do
if [ "${option}" == "${value}" ] ; then
found="1"
fi
done
if [ "${found}" == "0" ] ; then
echo_fail "Invalid input: (${key}) value: (${value}), valid options: ($( IFS=$", "; echo "${options[*]}" ))"
fi
}
#=======================================
# Main
#=======================================
#
# Validate parameters
echo_info "Configs:"
if [[ -n "$access_key_id" ]] ; then
echo_details "* access_key_id: ***"
else
echo_details "* access_key_id: [EMPTY]"
fi
if [[ -n "$secret_access_key" ]] ; then
echo_details "* secret_access_key: ***"
else
echo_details "* secret_access_key: [EMPTY]"
fi
echo_details "* upload_bucket: $upload_bucket"
echo_details "* upload_local_path: $upload_local_path"
echo_details "* acl_control: $acl_control"
echo_details "* set_acl_only_on_changed_objets: $set_acl_only_on_changed_objets"
echo_details "* aws_region: $aws_region"
echo
validate_required_input "access_key_id" $access_key_id
validate_required_input "secret_access_key" $secret_access_key
validate_required_input "upload_bucket" $upload_bucket
validate_required_input "upload_local_path" $upload_local_path
options=("public-read" "private")
validate_required_input_with_options "acl_control" $acl_control "${options[@]}"
options=("true" "no")
validate_required_input_with_options "set_acl_only_on_changed_objets" $set_acl_only_on_changed_objets "${options[@]}"
# this expansion is required for paths with ~
# more information: http://stackoverflow.com/questions/3963716/how-to-manually-expand-a-special-variable-ex-tilde-in-bash
eval expanded_upload_local_path="${upload_local_path}"
if [ ! -n "${upload_bucket}" ]; then
echo_fail 'Input upload_bucket is missing'
exit 1
fi
if [ ! -e "${expanded_upload_local_path}" ]; then
echo_fail "The specified local path doesn't exist at: ${expanded_upload_local_path}"
exit 1
fi
aclcmd='private'
if [ "${acl_control}" == 'public-read' ]; then
echo_details "ACL 'public-read' specified!"
aclcmd='public-read'
fi
if [[ "$aws_region" != "" ]] ; then
echo_details "AWS region (${aws_region}) specified!"
export AWS_DEFAULT_REGION="${aws_region}"
fi
s3_url="s3://${upload_bucket}"
export AWS_ACCESS_KEY_ID="${access_key_id}"
export AWS_SECRET_ACCESS_KEY="${secret_access_key}"
# do a sync -> Do NOT delete no longer existing objects
echo_info "$ aws s3 sync ${expanded_upload_local_path} ${s3_url} --acl ${aclcmd}"
aws s3 sync "${expanded_upload_local_path}" "${s3_url}" --acl ${aclcmd}
if [[ "${set_acl_only_on_changed_objets}" != "true" ]] ; then
echo_details "Setting ACL on every object, this can take some time..."
# `sync` only sets the --acl for the modified files, so we'll
# have to query the objects manually, and set the required acl one by one
IFS=$'\n'
for a_s3_obj_key in $(aws s3api list-objects --bucket "${upload_bucket}" --query Contents[].[Key] --output text)
do
echo_info "$ aws s3api put-object-acl --acl ${aclcmd} --bucket ${upload_bucket} --key ${a_s3_obj_key}"
aws s3api put-object-acl --acl ${aclcmd} --bucket "${upload_bucket}" --key "${a_s3_obj_key}"
done
unset IFS
else
echo_details "ACL is only changed on objects which were changed by the sync"
fi
echo_done "Success"
echo_details "Access Control set to: ${acl_control}"
if [[ -n ${AWS_DEFAULT_REGION} ]] ; then
echo_details "AWS Region: ${aws_region}"
fi
echo_details "Base URL: http://${upload_bucket}.s3.amazonaws.com/"