Skip to content

Instantly share code, notes, and snippets.

@kig
Created February 4, 2013 17:26
Show Gist options
  • Save kig/4708148 to your computer and use it in GitHub Desktop.
Save kig/4708148 to your computer and use it in GitHub Desktop.
A script to upload files to an S3 bucket. Sets mime type headers for HTML, CSS, JS, JPEG, GIF and PNG. Sets a thirty-year Expires header for images. Gzips HTML, CSS and JS.
#!/usr/bin/ruby
# USAGE: my_bucket_put FILE_1 [FILE_N...]
# Uploads $local_path_for_bucket/FILE_1 to my_bucket/FILE_1
# This script has very little configuration to keep it simple (and serve my limited needs).
# Create a pre-configured copy of this for all your buckets. Or add command line flags.
#
# Change this to your bucket name
bucketname = "my_bucket"
# Change this to the directory where your bucket files are.
local_path_for_bucket = "/Users/foo/my_bucket"
require 'cgi'
Dir.chdir(local_path_for_bucket)
def esc(s)
"'#{s.to_s.gsub("'", "'\\\\''")}'"
end
ARGV.each{|fn|
gzip = false
if fn =~ /\.jpe?g$/i
params = %("Content-Type: image/jpeg" "Expires: #{CGI.rfc1123_date(Time.now + 30*365*86400)}")
elsif fn =~ /\.png$/i
params = %("Content-Type: image/png" "Expires: #{CGI.rfc1123_date(Time.now + 30*365*86400)}")
elsif fn =~ /\.gif$/i
params = %("Content-Type: image/gif" "Expires: #{CGI.rfc1123_date(Time.now + 30*365*86400)}")
elsif fn =~ /\.html$/ or not fn.include?(".")
gzip = true
params = %("Content-Type: text/html")
elsif fn =~ /\.css$/
gzip = true
params = %("Content-Type: text/css")
elsif fn =~ /\.js$/
gzip = true
params = %("Content-Type: application/javascript")
end
STDERR.puts("Uploading #{fn}, #{params}")
if gzip
system(%(gzip -c #{esc(fn)} | aws --insecureaws put "Content-Encoding: gzip" #{params} #{bucketname}/#{esc(fn)}))
else
system(%(aws --insecureaws put #{params} #{bucketname} #{esc(fn)}))
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment