Last active
December 12, 2015 08:59
-
-
Save toolmantim/4747848 to your computer and use it in GitHub Desktop.
A command line tool for uploading a file to a S3 and printing the private, expiring URL to STDOUT. Stick it in your ~/bin, chmod u+x and upload like it's 1999.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # | |
| # Uploads a file to a S3 and prints a private, expiring URL to STDOUT | |
| # | |
| # Requirements: aws-s3 gem | |
| if ARGV.length != 3 | |
| abort "Usage: #{$0} FILE BUCKET EXPIRY_IN_SECONDS" | |
| end | |
| file, bucket, expiry = ARGV | |
| require 'aws/s3' | |
| unless (id = ENV['AMAZON_ACCESS_KEY_ID']) && (secret = ENV['AMAZON_SECRET_ACCESS_KEY']) | |
| abort "AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY env vars must be set" | |
| end | |
| AWS::S3::Base.establish_connection!(access_key_id: id, secret_access_key: secret) | |
| AWS::S3::Bucket.create(bucket) | |
| AWS::S3::S3Object.delete(file, bucket) if AWS::S3::S3Object.exists?(file, bucket) | |
| AWS::S3::S3Object.store(file, open(file), bucket) | |
| puts AWS::S3::S3Object.url_for(file, bucket, expires_in: expiry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment