Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Created June 3, 2020 10:10
Show Gist options
  • Save RoxasShadow/a8e7c5ae829fece20f0576ab097e1083 to your computer and use it in GitHub Desktop.
Save RoxasShadow/a8e7c5ae829fece20f0576ab097e1083 to your computer and use it in GitHub Desktop.
Try pinging Localstack and then reading and writing a file to/from a bucket on (Localstack or AWS) S3
#!/bin/bash
PORT=4572
# Ping Localstack up to 10 times every 0.5s.
# Using docker-wait wouldn't work as the server
# runs as blocking process and never terminates.
echo "Waiting for Localstack to be up..."
while ! nc -z localhost $PORT; do
sleep 0.5
((c++)) && ((c==10)) && c=0 && exit 1
done
echo "Localstack is up and running on the port $PORT!"
# Check writing permissions on the bucket.
# Localstack takes quite some time (3-4s) to finish
# spawning all of its components and execute the bucket
# creation in `config/docker/localstack`. In the meanwhile,
# it will still be pingable but the bucket won't be created
# if addressed too soon. So we wait for them 1s up to 15 times.
try_s3_write() {
bucket_status=$(AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test \
aws --endpoint-url=http://localhost:$PORT \
s3api put-object --key test --body test_file --bucket test-bucket 2>&1)
echo "${bucket_status}" | grep -q 'ETag'
return $?
}
touch test_file
echo "Testing S3 bucket permission on Localstack"
while ! try_s3_write; do
sleep 1
((c++)) && ((c==15)) && c=0 && exit 1
done
echo "Writing on Localstack S3 successful!"
try_s3_read() {
bucket_status=$(AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test \
aws --endpoint-url=http://localhost:$PORT \
s3api get-object --key test --bucket test-bucket /dev/stdout | sed 1q)
echo "${bucket_status}" | grep -q 'hello'
return $?
}
if [ ! try_s3_read ]; then echo "read failed"; else echo "read succeeded"; fi &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment