Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload_to_s3 command #57

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions bin/upload_artifact
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ fi
BASENAME=$(basename "$ARTIFACT_PATH")
KEY="$BUILDKITE_BUILD_ID/$BASENAME"

# If the bucket has transfer acceleration enabled, use it!
ACCELERATION_STATUS=$(aws s3api get-bucket-accelerate-configuration --bucket "$BUCKET" | jq '.Status' -r || true)

if [ "$ACCELERATION_STATUS" = "Enabled" ]; then
echo "Uploading with transfer acceleration"
aws s3 cp "$ARTIFACT_PATH" "s3://$BUCKET/$KEY" --endpoint-url https://s3-accelerate.amazonaws.com
else
aws s3 cp "$ARTIFACT_PATH" "s3://$BUCKET/$KEY"
fi
upload_to_s3 \
--file="$ARTIFACT_PATH" \
--bucket="$BUCKET" \
--name="$KEY"
67 changes: 67 additions & 0 deletions bin/upload_to_s3
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash -eu

function usage {
echo "Usage $0 --file=/path/to/file --name=file_name_on_S3 [--bucket=S3_bucket_name]"
}

# Option parsing logic based upon https://stackoverflow.com/a/28466267/809944
function exit_with_message() {
echo "$*" >&2; exit 1;
}

function needs_arg() {
if [ -z "$OPTARG" ]; then
exit_with_message "No argument for --$OPT option. Use format --$OPT=value.";
fi
}

while getopts "f:n:b:-:" OPT; do
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi

case "$OPT" in
f | file)
needs_arg; ARTIFACT_PATH="$OPTARG";;
b | bucket)
needs_arg; BUCKET="$OPTARG";;
n | name)
needs_arg; NAME="$OPTARG";;
??*) # bad long option
usage
exit_with_message "Illegal option --$OPT"
;;
?) # bad short option
usage
exit 1;;
esac
done

if [[ -z ${ARTIFACT_PATH:-} ]] || [[ -z ${NAME:-} ]]; then
usage
exit 1
fi

if [ ! -f "$ARTIFACT_PATH" ]; then
echo "No file found at $ARTIFACT_PATH"
exit 2
fi

BUCKET=${ARTIFACTS_S3_BUCKET-}

if [ -z "$BUCKET" ]; then
echo "You must either pass an S3 bucket via the -b or --bucket option, or set the \`ARTIFACTS_S3_BUCKET\` environment variable."
exit 3
fi

# If the bucket has transfer acceleration enabled, use it!
ACCELERATION_STATUS=$(aws s3api get-bucket-accelerate-configuration --bucket "$BUCKET" | jq '.Status' -r || true)

if [ "$ACCELERATION_STATUS" = "Enabled" ]; then
echo "Uploading with transfer acceleration"
aws s3 cp "$ARTIFACT_PATH" "s3://$BUCKET/$NAME" --endpoint-url https://s3-accelerate.amazonaws.com
else
aws s3 cp "$ARTIFACT_PATH" "s3://$BUCKET/$NAME"
fi