How to Share File Easily Using CLI with S3

Sometimes, we have to share file with college or client. If it’s a small file, we can use an email or some chat services like a Slack or Skype. But otherwise, we need to use some drive or share file services that kind of Dropbox for just sharing. Those kind of solutions are not bad, but if you are a “lazy” developer, you must be thinking that you want to finish everything inside your terminal.
If so, I recommend using Pre-Signed URLs with S3 as one method for sharing files.
If you use this method, you can use existing bucket without changing some settings for the bucket. And we can get complicated URL for the specific file. So, you can use this for the specific person who you want to share. Off course, you don’t have to login AWS console using a browser. you only need a terminal which is installed AWS command line interface. Let’s get your hands dirty!
Checking your environment
Before we do that, let’s check our environment. We need AWS CLI to access our bucket. My environment is following:
$ aws --version
aws-cli/1.11.0If you haven’t install AWS CLI, please try to install. There is a document on AWS document page. And also please make sure you set access key and secret access key to access AWS resources, especially bucket on S3.
$ cat ~/.aws/credentials
[default]
aws_access_key_id = [YOUR_ACCESS_KEY]
aws_secret_access_key = [YOUR_SECRET_ACCESS_KEY]Upload file with Pre-Signed URL
At first, let’s upload your file from your local to S3 bucket. The command is here:
$ aws s3 cp [FILE_PATH] s3://[YOUR_BUCKET_NAME]/tmp/After that, we can assign Pre-Signed URL for that.
aws s3 presign s3://[YOUR_BUCKET_NAME]/tmp/[YOUR_UPLOADED_FILE_NAME] --expires-in [TIME_BY_SECONDS]When you create Pre-Signed URL, you can pass — expires-in option. This indicates that how much time will it opened with Pre-Signed URL.
If you get success response, you can see URL which is accessible for anyone who knows this URL.
https://[YOUR_BUCKET_NAME].s3.amazonaws.com/tmp/[YOUR_FILE_NAME]?AWSAccessKeyId=XXXXXXXX&Expires=XXXXXXXX&Signature=XXXXXXXXThat’s all! you just share this URL with people who you want to share file.
And I highly recommend setting Object Lifecycle Management to reduce your bucket size. It means, saving you wallet. You can set that for the specific directory, in this case, tmp directory is the target.
Make it easier
I usually use this method, but I need to run 2 commands and copy using my cursor. It’s painful. So I defined a method to automate those stuff. You can automate those tasks just only define this function on your .zshrc.
share_file_s3() {
if [ ! -n "$1" ]; then
echo "Usage: $0 [file]"
return
fi defaultExpireTime=3600 // 1hour if [ -n "$2" ]; then
defaultExpireTime=$2
fi aws s3 cp $1 s3://[YOUR_BUCKET_NAME]/tmp/
aws s3 presign s3://[YOUR_BUCKET_NAME]/tmp/$1 --expires-in $defaultExpireTime | pbcopy
}
You can use this like a:
$ share_file_s3 [YOUR_FILE]
# after that, generated URL is already registered to your clipboardThat’s so simple, isn’t it?
If you don’t have any kind of method of sharing the file so far, please think this as one of the easiest ways to share file 👍
