Skip to content

Instantly share code, notes, and snippets.

@simon-wenmouth
Last active December 21, 2015 06:49
Show Gist options
  • Save simon-wenmouth/6266773 to your computer and use it in GitHub Desktop.
Save simon-wenmouth/6266773 to your computer and use it in GitHub Desktop.
Script to reproduce the put-object / location-constraint issue with the AWS CLI
#!/bin/bash
#
# Command Line Options:
#
# -d | use --debug flag
# -n | use --no-verify-ssl flag
# -p profile_name | specify the AWS CLI profile
# -r region_name | specify the AWS region for the S3 bucket
# -v | enable bash verbosity
declare -a valid_region_names=('us-east-1' 'us-west-2' 'us-west-1' 'eu-west-1' 'ap-southeast-1' 'ap-southeast-2' 'ap-northeast-1' 'sa-east-1');
aws_opts=""
region_name=
while getopts p:r:nv opt; do
case $opt in
r)
region_name=$OPTARG
;;
d)
aws_opts+=" --debug"
;;
n)
aws_opts+=" --no-verify-ssl"
;;
p)
aws_opts+=" --profile $OPTARG"
;;
v)
set -o verbose
;;
esac
done
shift $((OPTIND - 1))
bucket_name=`openssl rand -hex 8`
if [ -z "$region_name" ]; then
aws s3 create-bucket $aws_opts --bucket $bucket_name
else
have_valid_region_name=false
for valid_region_name in "${valid_region_names[@]}"; do
if [ "$valid_region_name" == "$region_name" ]; then
have_valid_region_name=true
fi
done
if ! $have_valid_region_name; then
echo "'$region_name' is not a recognized region"
exit -1;
fi
aws s3 create-bucket $aws_opts --bucket $bucket_name --create-bucket-configuration "{\"LocationConstraint\": \"$region_name\"}"
fi
key_name=object_for_bucket_$bucket_name.txt
openssl rand -hex 16 > $key_name
aws s3 put-object $aws_opts --bucket $bucket_name --key $key_name --body $key_name --content-type text/plain
until aws s3 list-objects $aws_opts --bucket $bucket_name | grep -zq $key_name; do
sleep 60
((minutes++))
echo "Retrying Put-Object ($minutes) ..."
aws s3 put-object $aws_opts --bucket $bucket_name --key $key_name --body $key_name --content-type text/plain
done
aws s3 delete-object $aws_opts --bucket $bucket_name --key $key_name
aws s3 delete-bucket $aws_opts --bucket $bucket_name
rm $key_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment