Skip to content

Instantly share code, notes, and snippets.

@dpickett
Created January 20, 2011 19:38
Show Gist options
  • Save dpickett/788471 to your computer and use it in GitHub Desktop.
Save dpickett/788471 to your computer and use it in GitHub Desktop.
begin
require 'aws/s3'
rescue LoadError => e
e.message << " (You may need to install the aws-s3 gem)"
raise e
end
class SitemapS3Publisher
def generate
sitemap.create
end
def upload
Dir.glob(sitemap.public_path.join("*xml.gz")).each do |f|
self.class.store_file(f)
end
end
def publish
generate
upload
end
def self.store_file(file)
connect_to_storage_provider!
AWS::S3::S3Object.store("sitemaps/#{File.basename(file)}",
File.read(file),
bucket_name,
{
:access => default_access,
})
end
protected
def sitemap
SitemapGenerator::Sitemap
end
def self.bucket_name
storage_provider_configuration["bucket"]
end
def self.default_access
:public_read
end
def self.connect_to_storage_provider!
AWS::S3::Base.establish_connection!(storage_provider_credentials)
end
def self.storage_provider_configuration
@storage_provider_configuration ||= YAML::load(
File.read(storage_provider_configuration_file)).to_hash[Rails.env]
end
def self.storage_provider_credentials
if @storage_provider_credentials.nil?
@storage_provider_credentials = {}
[
"access_key_id",
"secret_access_key"
].each do |c|
@storage_provider_credentials[c.to_sym] = storage_provider_configuration[c]
end
end
@storage_provider_credentials
end
def self.storage_provider_configuration_file
Rails.root.join("config/s3.yml")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment