Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Last active December 12, 2015 08:59
Show Gist options
  • Select an option

  • Save toolmantim/4747848 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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