Skip to content

Instantly share code, notes, and snippets.

@mRB0
Last active March 18, 2017 16:49
Show Gist options
  • Save mRB0/390baafd3cc2310a0ba8835f195e4c62 to your computer and use it in GitHub Desktop.
Save mRB0/390baafd3cc2310a0ba8835f195e4c62 to your computer and use it in GitHub Desktop.
A shell script replacement for the Dropbox public folder, using S3 for storage and serving.
#!/usr/bin/env bash
#
# Usage:
#
# share.bash file1 file2 file3 ...
#
#
# Simple setup:
#
# 1. Install the AWS CLI tool:
# http://docs.aws.amazon.com/cli/latest/userguide/installing.html
#
# 2. Sign up for an Amazon Web Services account at https://aws.amazon.com/
# (don't forget to set up billing to prevent service interruptions)
#
# 3. Navigate to Amazon S3 on the AWS website.
#
# 4. Create an S3 bucket; give it any name you like, create it in any region
# you like, and copy the name into the "S3_BUCKET" setting into this file.
#
# 5. In the top-right of the AWS website, click your name and open
# "My Security Credentials"
#
# 6. Open "Access Keys (Access Key ID and Secret Access Key)"
#
# 7. Click "Create New Access Key"
# IMPORTANT: This access key allows FULL ACCESS to your account! Keep it
# secret! Don't share this file with ANYONE after you've entered
# it!
# A better option is to create an IAM user with access only to S3
# but that's beyond the scope of these "Simple setup" instructions.
#
# 8. Copy the new Access Key ID and Secret Access Key into this file.
#
##
## Settings
##
# Enter your S3 bucket name
export S3_BUCKET='mysweetbucket'
# Enter your Amazon access key ID
export AWS_ACCESS_KEY_ID='AKIA...etc'
# Enter your Amazon secret access key
export AWS_SECRET_ACCESS_KEY='aAbuhoJFWNGK/etc'
##
## Don't change anything after this point!
##
if [[ $# == 0 ]]
then
echo "Usage: $0 file1 file2 file3 ..."
exit 1
fi
BUCKET_REGION="$(aws s3api get-bucket-location --bucket "$S3_BUCKET" --query 'LocationConstraint' --output text)"
export AWS_DEFAULT_REGION="$BUCKET_REGION"
i=0
for filename in "$@"
do
if [[ $i > 0 ]]; then
echo
fi
echo "upload $filename"
aws s3 cp --storage-class STANDARD_IA --acl public-read "$filename" s3://"$S3_BUCKET"/
if [[ $? == 0 ]]; then
urlescaped="$(python -c 'import urllib, sys; print(urllib.quote(sys.argv[1]))' "$filename")"
echo "https://s3.${BUCKET_REGION}.amazonaws.com/${S3_BUCKET}/$urlescaped"
fi
i=$((i + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment