-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.sh
172 lines (152 loc) · 5.21 KB
/
s3.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
print_help_s3() {
print_command_help "s3" "cp ls mb"
}
s3_ls() {
[[ "${_positionals[2]:-}" = "help" ]] && print_help_s3 && exit 0
s3url="${_positionals[2]:-}" # s3url is optional
if [[ "${s3url}" = "" ]]; then
bucket=""
key=""
formatter="xml_to_text_for_buckets"
else
if is_s3url "${s3url}"; then
bucket="$(get_bucket_from_s3url "${s3url}")"
key="$(get_key_from_s3url "${s3url}")"
else
bucket="$(get_bucket_from_s3url "s3://${s3url}")"
key="$(get_key_from_s3url "s3://${s3url}")"
fi
formatter="xml_to_text_for_keys"
fi
if [[ "${dryrun}" = "echo" ]]; then
formatter="cat"
fi
http_method="GET"
content_sha256="${empty_string_sha256}"
date="$(date -u +%Y%m%dT%H%M%SZ)"
short_date="${date%%T*}"
request_url="$(create_request_url "${service}" ls "${_arg_endpoint_url}" "${region}" "${bucket}" "${key}")"
if [[ "${_arg_no_sign_request}" = "on" ]]; then
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail "${request_url}" | "${formatter}"
else
headers=("host:$(get_host_from_request_url "$request_url")" "x-amz-content-sha256:${content_sha256}" "x-amz-date:${date}" "${security_token_header:-}")
set_headers
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail \
"${request_url}" \
-H "Authorization: ${authorization_header}" \
${curl_headers} | "${formatter}"
fi
}
s3_mb() {
[[ "${_positionals[2]:-}" = "help" ]] && print_help_s3 && exit 0
if [[ "${#_positionals[@]}" -lt 3 ]]; then
_PRINT_HELP=yes die "$0: error: the following arguments are required: paths"
fi
if ! is_s3url "${_positionals[2]}"; then
_PRINT_HELP=no die "
<S3Uri>
Error: Invalid argument type"
fi
_arg_bucket="$(get_bucket_from_s3url "${_positionals[2]:-}")"
s3api_create-bucket
echo "make_bucket: ${_arg_bucket}"
}
s3_cp() {
[[ "${_positionals[2]:-}" = "help" ]] && print_help_s3 && exit 0
if [[ "${#_positionals[@]}" -lt 4 ]]; then
_PRINT_HELP=yes die "$0: error: the following arguments are required: paths"
else
source="${_positionals[2]}"
destination="${_positionals[3]}"
fi
if ! is_one_s3url_provided "$source" "$destination"; then
die "usage: $0 s3 cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath>
Error: Invalid argument type" # or <S3Uri> <S3Uri> (not yet implemented)
fi
if ! is_s3url "$source"; then
if [[ ! -r "$source" ]]; then
die "The user-provided path ${source} does not exist."
fi
fi
if is_s3url "$source"; then
http_method="GET"
bucket="$(get_bucket_from_s3url "$source")"
key="$(get_key_from_s3url "$source")"
content_md5=""
content_type=""
content_sha256="${empty_string_sha256}"
request_url="$(create_request_url "${service}" cp "${_arg_endpoint_url}" "${region}" "${bucket}" "${key}")"
else
http_method="PUT"
bucket="$(get_bucket_from_s3url "${destination}")"
key="$(get_key_from_s3url "${destination}")"
content_md5="content-md5:$(md5_base64 "${source}")"
content_type="$(get_mime "${source}" "${_arg_content_type}" "${_arg_no_guess_mime_type}")"
if [[ -z "$key" ]]; then
key="${source##*/}"
fi
request_url="$(create_request_url "${service}" cp "${_arg_endpoint_url}" "${region}" "${bucket}" "${key}")"
protocol="$(get_protocol_from_request_url "${request_url}")"
if [[ "${protocol}" = "https" ]];then
content_sha256="UNSIGNED-PAYLOAD"
else
content_sha256="$(sha256 "${source}")"
fi
fi
if [[ "${_arg_no_sign_request}" = "on" ]]; then
if [[ "${http_method}" == "PUT" ]]; then
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail -X "${http_method}" "${request_url}" --data-binary "@${source}" -o /dev/null
echo "upload: ${source} to s3://${bucket}/${key}"
else
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail "${request_url}" -o "${destination}"
echo "download: ${source} to ${destination}"
fi
else
date="$(date -u +%Y%m%dT%H%M%SZ)"
short_date="${date%%T*}"
headers=("host:$(get_host_from_request_url "$request_url")" "x-amz-content-sha256:${content_sha256}" "x-amz-date:${date}" "${content_md5}" "${content_type}" "${security_token_header:-}" "${sse_header:-}" "${storage_class_header:-}" "${acl_header:-}")
set_headers
if [[ "${http_method}" == "PUT" ]]; then
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail -X "${http_method}" \
"${request_url}" \
-H "Authorization:${authorization_header}" \
${curl_headers} \
--data-binary "@${source}" -o /dev/null
echo "upload: ${source} to s3://${bucket}/${key}"
else
# shellcheck disable=SC2086
${dryrun} curl ${curl_output} --fail \
"${request_url}" \
-H "Authorization: ${authorization_header}" \
${curl_headers} \
-o "${destination}"
echo "download: ${source} to ${destination}"
fi
fi
}
s3() {
case "${_arg_subcommand:-}" in
cp)
s3_cp
;;
ls)
s3_ls
;;
mb)
s3_mb
;;
help)
print_help_s3
exit 0
;;
*)
_PRINT_HELP=yes die "$0: error: argument subcommand: Invalid choice, valid choices are:
ls | cp"
;;
esac
}