Skip to content

Instantly share code, notes, and snippets.

@mikecarroll
Forked from rorra/sitemap_generator.rb
Last active December 25, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecarroll/5223800 to your computer and use it in GitHub Desktop.
Save mikecarroll/5223800 to your computer and use it in GitHub Desktop.
Some changes made to make this work with the most recent version of sitemap_generator. Also, changed from using an s3.yml file to just using the Heroku S3 config variables directly.
require 'aws'
class Rake::Task
def replace &block
@actions.clear
prerequisites.clear
enhance &block
end
end
namespace 'sitemap' do
desc 'Upload the sitemap files to S3'
task :upload_to_s3 => :environment do
# Load credentials
bucket_name = ENV['S3_BUCKET_NAME']
puts "BUCKET: #{bucket_name}"
# Establish S3 connection
AWS.config({
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
})
Dir.entries(File.join(Rails.root, "tmp", "sitemaps")).each do |file_name|
next if ['.', '..'].include? file_name
path = "sitemaps/#{file_name}"
file = File.join(Rails.root, "tmp", "sitemaps", file_name)
begin
s3 = AWS::S3.new
bucket = s3.buckets.create(bucket_name)
object = bucket.objects[path]
object.write(:file => file)
rescue Exception => e
raise
end
puts "Saved #{file_name} to S3"
end
end
end
Rake::Task["sitemap:create"].enhance do
Rake::Task["sitemap:upload_to_s3"].invoke
end
Rake::Task[:'sitemap:refresh'].replace do
bucket_name = ENV['S3_BUCKET_NAME']
SitemapGenerator::Sitemap.ping_search_engines("https://#{bucket_name}.s3.amazonaws.com/sitemaps/sitemap_index.xml.gz")
end
@milanij
Copy link

milanij commented Jan 17, 2015

Thank you for posting this edit to rorra's work. It was exactly what I needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment