The Plan
As I mentioned yesterday, I’m getting tired of manually uploading files to S3 to share. Today I wrote a short bash script to handle this task in one simple command: pp-upload <filename>.
Post Mortem
While looking at a bash script isn’t pretty I’ll throw it up here.
#! /bin/bash main() { FILE="$1" BUCKET=aaronpeddle.com if [ -f $FILE ]; then aws s3 cp $FILE s3://$BUCKET/ --acl public-read URL="https://s3-us-west-2.amazonaws.com/$BUCKET/`basename $FILE`" echo $URL else echo "File $FILE does not exist." fi } if [ $# -eq 1 ] then main $1 else echo "script takes one argument - the file" fi
Bash has so many weird gotchas, it always takes me longer to write bash script than I expect. Today I ran into something weird where assigning the results of “basename $FILE_PATH” to a variable was unsetting $FILE_PATH. I had no idea why this was happening and given the 20 minute time restraint I didn’t have time to figure it out. If anyone reading this has a theory I’d love to hear it.
Key Learnings/Observations
- Bash Scripting will take longer than I expect. In the future I’ll need to budget more time for tasks then I intuitively think I need.
- There’s a significant amount of boiler plate code for a basic script. I wonder if making a skeleton code generator script would be useful here.
I’m quite happy with this one. This is definitely my most useful project yet. I’ve symlinked this script onto my PATH and will be using this script frequently for future blog posts. For tomorrow’s post hopefully I’ll get to test it out in practice.
-Aaron