在Sitemap_generator中为Microsoft Azure编写适配器

时间:2019-01-29 10:51:15

标签: ruby-on-rails azure sitemap-generator-gem

我正在使用sitemap_generator在我的RoR项目中生成站点地图。到目前为止一切正常。我将我的项目托管在Heroku上,这不允许写入本地文件系统。我仍然需要进行一些写操作访问,因为需要在上传之前写出站点地图文件。但是我必须使用Microsoft azure来存储我的站点地图。sitemap_generator中列出的适配器不包括azure。有人可以指出正确的方向来编写用于azure的适配器。

参考this文章中的“配置载波”,我对代码进行了一些更改。

但是我不确定仅编辑initialiazer文件会有所帮助。在上面的文章中,Carrierwave指向WaveAdapter here,该适配器使用CarrierWave :: Uploader :: Base上传到CarrierWave支持的任何服务< / p>

config / initializers / azure.rb

Azure.configure do |config|
    config.cache_dir = "#{Rails.root}/tmp/"
    config.storage = :microsoft_azure
    config.permissions = 0666
    config.microsoft_azure_credentials = {
       :provider               => 'azure',
       :storage_account_name      => 'your account name',
       :storage_access_key  => 'your key',
    }
    config.azure_directory  = 'container name'
end

请帮助!

1 个答案:

答案 0 :(得分:0)

我从the S3 adapterAzure's ruby example复制了我的设置

将天蓝色blob宝石添加到您的Gemfile中: gem 'azure-storage-blob'

创建配置/初始化/ sitemap_generator / azure_adapter.rb:

require 'azure/storage/blob'

module SitemapGenerator
  # Class for uploading sitemaps to Azure blobs using azure-storage-blob gem.
  class AzureAdapter
    #
    # @option :storage_account_name [String] Your Azure access key id
    # @option :storage_access_key [String] Your Azure secret access key
    # @option :container [String]
    def initialize
      @storage_account_name = 'your account name'
      @storage_access_key = 'your key'
      @container = 'your container name (created already in Azure)'
    end

    # Call with a SitemapLocation and string data
    def write(location, raw_data)
      SitemapGenerator::FileAdapter.new.write(location, raw_data)

      credentials = {
        storage_account_name: @storage_account_name,
        storage_access_key: @storage_access_key
      }

      client = Azure::Storage::Blob::BlobService.create(credentials)
      container = @container
      content = ::File.open(location.path, 'rb') { |file| file.read }
      client.create_block_blob(container, location.filename, content)
    end
  end
end
  • 确保您在Azure中创建的容器是一个“斑点”容器,容器不公开,但内部的斑点是。

,然后在config / sitemaps.rb中:

SitemapGenerator::Sitemap.sitemaps_host = 'https://[your-azure-address].blob.core.windows.net/'
SitemapGenerator::Sitemap.sitemaps_path = '[your-container-name]/'
SitemapGenerator::Sitemap.adapter = SitemapGenerator::AzureAdapter.new

应该这样做!