Skip to content

Instantly share code, notes, and snippets.

@bobbus
Forked from dpickett/sitemap_s3_publisher.rb
Created April 21, 2011 14: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 bobbus/934695 to your computer and use it in GitHub Desktop.
Save bobbus/934695 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