A simple client library aim to sync assets to Windows Azure, using a modified version of the great waz-storage gem
Add to your gemfile :
gem 'waz-storage', :git=>"https://github.com/gmontard/waz-storage.git" gem 'waz-sync'
Create a config file to /app/config/azure.yml to hold your Azure credentials
development: account_name: NAME access_key: KEY staging: account_name: NAME access_key: KEY production: account_name: NAME access_key: KEY
azure = WazSync.new() ["images", "javascripts", "stylesheets"].each{|folder| container = azure.find_or_create_container(folder) Dir.glob("#{Rails.root.to_s}/public/#{folder}/**/*").each do |file| if File.file?(file) filename = file.gsub("/public/#{folder}/", "").gsub("#{Rails.root.to_s}", "") azure.send_or_update(container, file, filename) end end }
This simple code will sync all your assets unders “images”, “javascripts” and “stylesheets” to Windows Azure CDN
There is a rake task included to this Gem. To use it you first have to include this line into your Rakefile
require 'waz_sync/tasks'
Then you can use the rake task
rake waz:sync
By default it does sync your images, javascripts and stylesheets folders, but you can specify which folder to sync
rake waz:sync folders=images,foo,bar
If you use capistrano to deploy you app it’s a good idea to sync you assets during each deploy. Add this line to your deploy.rb
require "waz_sync/recipes"
Then you can add these callbacks
before "deploy:stop", "waz_sync:sync" before "deploy:start", "waz_sync:sync" before "deploy:restart", "waz_sync:sync"
There is also a variable :waz_sync_args where you can specify which folder to sync
set :waz_sync_args, "images,foo,bar"
This Gem use a modified version of the waz-storage gem that you can find there : github.com/johnnyhalife/waz-storage
You can find my modified version here : github.com/gmontard/waz-storage
If you also want to automatically sync your assets cached file (javascripts && stylesheets) created using the include rails helper with :cache condition,
javascript_include_tag :all, :cache => true
you can take a look at this override lib file (tested on Rails 2.3.14) :
##### ## Add this line to your environment.rb file # require 'lib/override_assetfile_cache.rb' #### module ActionView module Helpers module AssetTagHelper def write_asset_file_contents(joined_asset_path, asset_paths) FileUtils.mkdir_p(File.dirname(joined_asset_path)) File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) } # Set mtime to the latest of the combined files to allow for # consistent ETag without a shared filesystem. mt = asset_paths.map { |p| File.mtime(asset_file_path(p)) }.max File.utime(mt, mt, joined_asset_path) ###################### ### Azure CDN Sync ### ###################### begin azure = WazSync.new() folder = joined_asset_path.match(/^.*public\/([^\/]*)\/(.*)$/)[1] filename = joined_asset_path.match(/^.*public\/([^\/]*)\/(.*)$/)[2] container = azure.find_or_create_container(folder) azure.send_or_update(container, joined_asset_path, filename) rescue Rails.logger.info("Azure Sync Failed") end ###################### end end end end
Tests !!!!
Written by Guillaume Montard for Vodeclic SAS (www.vodeclic.com)
Released under the MIT License: www.opensource.org/licenses/mit-license.php