umorpha-boxes/bin/vultr-upload

38 lines
1006 B
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
set -euE -o pipefail
main() {
local arg_src_file arg_dsts3_url
arg_src_file="$1"
arg_dst_s3url="$2"
printf >&2 ":: Uploading %q to %q\n" "$arg_src_file" "$arg_dst_s3url"
local hostname
hostname="$(bin/vultr-s3cmd hostname)"
bucket=${arg_dst_s3url,,}
bucket=${bucket#s3://}
bucket=${bucket%%/*}
bin/vultr-s3cmd mb "s3://$bucket" >&2
md5_s3=$(bin/vultr-s3cmd ls --list-md5 "$arg_dst_s3url" | awk '{print $4}')
md5_local=$(md5sum "$arg_src_file" | awk '{print $1}')
if [[ "$md5_s3" == "$md5_local" ]]; then
printf >&2 ":: Already uploaded, nothing to do\n"
else
printf >&2 ":: Uploading: md5_s3:%q != md5_local:%q\n" "$md5_s3" "$md5_local"
time bin/vultr-s3cmd put --continue --progress --acl-public --verbose \
"$arg_src_file" "$arg_dst_s3url" >&2
fi
dst_httpurl="https://${bucket}.${hostname}/${arg_dst_s3url#s3://${bucket}/}"
printf '%s\n' "$dst_httpurl"
}
main "$@"