Skip to content

Instantly share code, notes, and snippets.

@hawkeyetwolf
Last active June 20, 2017 15:36
Show Gist options
  • Save hawkeyetwolf/352b110e710b4c2ab358 to your computer and use it in GitHub Desktop.
Save hawkeyetwolf/352b110e710b4c2ab358 to your computer and use it in GitHub Desktop.
Upload files to S3
#!/bin/sh
# Shortcut to the s3 console command.
s3='/usr/local/bin/aws s3'
# Define the AWS action: move (mv) or copy (cp).
action=$1
# Remove action from list of file arguments.
shift
# Define the base path
uri='s3://deraps/f'
# Upload each file.
for f do
# Initialize some variables for later use.
basename=$(basename "$f")
filename="${basename%.*}"
# Convert markdown notes to HTML.
extension="${basename##*.}"
realpath=$(grealpath "$f")
dirname=$(dirname "$realpath")
if [ "md" == "$extension" ]
then
upload="$filename.html"
original="$f"
f="/tmp/$upload"
grip "$original" --export "$f"
else
# Append checksum to prevent overwriting files with the same name.
# Note that the aws "ls" command returns partial matches.
results=$($s3 ls "$uri/$basename")
if [ $? -eq 0 ] && [[ $results == *$basename* ]]
then
# File with same name already exists; append checksum.
checksum=$(sum "$f" | awk '{print $1}')
# Make sure file has an extension.
if [[ $basename == *.* ]]
then
upload="$filename.$checksum.$extension"
else
upload="$filename-$checksum"
fi
# For first time uploads, just use the filename as-is.
else
upload="$basename"
fi
fi
# Transliterate.
# @TODO Include transliteration for all characters that get URL-encoded.
upload=$(echo $upload | tr ' ' '-')
# Push to S3 with encryption and the desired storage class.
$s3 $action "$f" "$uri/$upload" --sse --storage-class STANDARD_IA
# Keep track of uploaded paths.
path="https://deraps.s3.amazonaws.com/f/$upload"
[[ "$paths" ]] && paths="$paths $path" || paths="$path"
done
# Copy path(s) to clipboard.
echo "$paths\c" | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment