Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Created October 27, 2013 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EronHennessey/7177556 to your computer and use it in GitHub Desktop.
Save EronHennessey/7177556 to your computer and use it in GitHub Desktop.
Publish a local file to an AWS S3 bucket (make it public, give it a URL).
require 'aws-sdk'
require 'yaml'
CONFIG_FILE_NAME = 'aws-config.txt'
def do_confirm(phrase)
print "#{phrase} (Y/N): "
# you *must* use $stdin with gets if you have command-line arguments.
# Otherwise, gets receives streaming input and doesn't wait for the terminal.
a = $stdin.gets.strip.downcase
return a == 'y'
end
# make sure the user entered:
# 1. a file to publish
# 2. the bucket to publish it in.
if ARGV.count != 2
puts "Usage: publish_to_s3 <file> <s3_bucket+path>"
puts "Both arguments are required!"
exit
end
# get the args. The `bucket_name` arg might have an object appended to it after
# the first slash character...
file_name, bucket_name = ARGV
# split the bucket name after the first slash character. Anything afterward is
# considered the S3 object name.
bucket_name, object_name = bucket_name.split('/', 2)
# make sure that `file_name` refers to an actual file...
unless File.exists?(file_name)
puts "** local file does not exist: #{file_name}"
exit
end
# if no object name was provided (appended to the bucket name after '/'), then
# use the file name as the object name. We'll publish the file with the same
# name that it has on the local file system.
if object_name.nil?
object_name = File.basename(file_name)
end
# Load aws-config.txt to see if there are AWS credentials there.
#
# We're expecting a YAML-formatted file like this:
#
# ---
# access_key_id: ACCESSKEYID
# secret_access_key: SECRETACCESKEY
#
begin
config_file = File.open(CONFIG_FILE_NAME) { |f| f.read }
options = YAML.load(config_file)
AWS.config(options)
rescue
puts "** Hmmm, no config file (#{CONFIG_FILE_NAME})!"
# without setting AWS credentials specifically, the AWS SDK will try to load
# them from the environment variables. Check the environment variables, just
# to make sure...
if ENV['AWS_ACCESS_KEY_ID'].nil? or ENV['AWS_SECRET_ACCESS_KEY'].nil?
puts "** No AWS access information in environment, either. Aborting!"
exit
end
end
# Create a new S3 object
s3 = AWS::S3.new
s3bucket = s3.buckets[bucket_name]
unless s3bucket.exists?
puts "** Bucket \"#{bucket_name}\" does not exist on S3!"
exit unless do_confirm("Do you want me to create it?")
end
puts "publishing:"
puts " #{file_name}"
puts " -> S3://#{bucket_name}/#{object_name}"
exit unless do_confirm("OK?")
# does the object already exist? If so, we should update it. If not, we'll
# create a new object.
s3obj = s3bucket.objects[object_name]
if s3obj.exists?
puts "** updating existing object"
else
puts "** creating new object"
end
print "** writing object... "
s3obj.write(File.new(file_name, 'rb').read)
puts "complete!"
s3obj.acl = :public_read
puts "** object has public_read access"
public_url = s3obj.public_url({:secure => false})
puts "You can now access your object with the URL:"
puts " #{public_url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment