-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-to-s3
executable file
·78 lines (65 loc) · 2.51 KB
/
upload-to-s3
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
#!/usr/bin/env bash
set -euo pipefail
bin="$(dirname "$0")"
main() {
local quiet=0
for arg; do
case "$arg" in
--quiet)
quiet=1
shift;;
*)
break;;
esac
done
local src="${1:?A source file is required as the first argument.}"
local dst="${2:?A destination s3:// URL is required as the second argument.}"
local cloudfront_domain="${3:-}"
local s3path="${dst#s3://}"
local bucket="${s3path%%/*}"
local key="${s3path#*/}"
local src_hash dst_hash no_hash=0000000000000000000000000000000000000000000000000000000000000000
src_hash="$("$bin/sha256sum" < "$src")"
dst_hash="$(aws s3api head-object --bucket "$bucket" --key "$key" --query Metadata.sha256sum --output text 2>/dev/null || echo "$no_hash")"
if [[ $src_hash != "$dst_hash" ]]; then
# The record count may have changed
src_record_count="$(wc -l < "$src")"
echo "Uploading $src → $dst"
if [[ "$dst" == *.gz ]]; then
gzip -c "$src"
elif [[ "$dst" == *.xz ]]; then
xz -2 -T0 -c "$src"
elif [[ "$dst" == *.zst ]]; then
zstd -T0 -c "$src"
else
cat "$src"
fi | aws s3 cp --no-progress - "$dst" --metadata sha256sum="$src_hash",recordcount="$src_record_count" "$(content-type "$dst")"
if [[ -n $cloudfront_domain ]]; then
echo "Creating CloudFront invalidation for $cloudfront_domain/$key"
if ! "$bin"/cloudfront-invalidate "$cloudfront_domain" "/$key"; then
echo "CloudFront invalidation failed, but exiting with success anyway."
fi
fi
if [[ $quiet == 1 ]]; then
echo "Quiet mode. No Slack notification sent."
exit 0
fi
if ! "$bin"/notify-slack "Updated $dst available."; then
echo "Notifying Slack failed, but exiting with success anyway."
fi
else
echo "Uploading $src → $dst: files are identical, skipping upload"
fi
}
content-type() {
case "$1" in
*.tsv) echo --content-type=text/tab-separated-values;;
*.csv) echo --content-type=text/comma-separated-values;;
*.ndjson) echo --content-type=application/x-ndjson;;
*.gz) echo --content-type=application/gzip;;
*.xz) echo --content-type=application/x-xz;;
*.zst) echo --content-type=application/zstd;;
*) echo --content-type=text/plain;;
esac
}
main "$@"