Skip to content

Instantly share code, notes, and snippets.

@JeroenBoersma
Last active November 10, 2020 13:19
Show Gist options
  • Save JeroenBoersma/87e29fd4aa06ec42216c80a6e3649fa5 to your computer and use it in GitHub Desktop.
Save JeroenBoersma/87e29fd4aa06ec42216c80a6e3649fa5 to your computer and use it in GitHub Desktop.
Docker AWS (with love)
#!/bin/bash
CWD=${PWD};
aws() {
docker run --rm -it -v ${HOME}/.aws:/root/.aws -v ${CWD}:/mnt --workdir=/mnt amazon/aws-cli "$@"
return $?;
}
if [ -z "`which jq`" ]; then
jq() {
cat -;
}
fi
helpers() {
local h=$1;
shift;
get_bucket() {
echo $1 | sed -e 's#^s3://\([^/]*\)/.*$#\1#g';
}
strip_bucket() {
echo $1 | sed -e 's#^s3://\([^/]*\)/\(.*\)$#\2#g';
}
format_versions() {
echo "$@" | jq -r '.Versions | map([.VersionId, .LastModified, .Size, .Key] | join("\t")) | .[]';
}
$h "$@";
}
main() {
local command=$1 internal='love-'$1;
shift;
love-ls-versions() {
local request=$1;
local bucket=$(helpers get_bucket ${request});
local prefix=$(helpers strip_bucket ${request});
local result=$(aws s3api list-object-versions --bucket "${bucket}" --prefix "${prefix}");
helpers format_versions "${result}";
}
love-cp-version() {
local request=$1;
local version=$2;
local bucket=$(helpers get_bucket ${request});
local key=$(helpers strip_bucket ${request});
local outputfile=$(basename ${key});
if [ -z "${version}" ]; then
love-ls-versions "${request}";
return $?;
fi
aws s3api get-object --version-id "${version}" --bucket "${bucket}" --key "${key}" "${outputfile}";
}
type ${internal} >/dev/null 2>&1 && ${internal} "$@" || aws "${command}" "$@";
}
main "$@";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment