Skip to content

Instantly share code, notes, and snippets.

@Maniacal
Created May 23, 2012 17:56
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 Maniacal/2776680 to your computer and use it in GitHub Desktop.
Save Maniacal/2776680 to your computer and use it in GitHub Desktop.
Log mover script for moving logs to s3
#!/usr/bin/env ruby
#
# Script for pushing logs to s3
#
#require 'rubygems'
#require 'yaml'
require 'fog'
config_file = File.join(File.dirname(__FILE__),"logmoverconfig.yml")
unless File.exist?(config_file)
puts <<END
Config file logmoverconfig.yml missing. Please create this file in the
same directory as the logmover.rb script and use the following format:
access_key_id: YOUR_ACCESS_KEY_ID
secret_access_key: YOUR_SECRET_ACCESS_KEY
END
exit 1
end
config = YAML.load(File.read(config_file))
unless config.kind_of?(Hash)
puts <<END
logmoverconfig.yml is formatted incorrectly. Please use the following format:
access_key_id: YOUR_ACCESS_KEY_ID
secret_access_key: YOUR_SECRET_ACCESS_KEY
END
exit 1
end
(target, filename) = ARGV
unless target && filename
puts <<END
Usage: __FILE__ <s3path> <file>
target = full path (including file name) where the file is to be uploaded.
(ex, mybucket/myfolder/myfile.txt)
file = full path to file to be uploaded. (ex, /home/me/myfile.txt)
END
exit 1
end
# Connect to AWS
connection = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => config['aws_access_key_id'],
:aws_secret_access_key => config['aws_secret_access_key']
})
# Create required vars from the inputs
s3path = target.split("/")
bucket, key = s3path[0],s3path.last(s3path.length-1).join("/")
# Upload the file
connection.put_object(bucket, key, File.read(filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment