【问题标题】:Write adapter for microsoft azure in sitemap_generator在 sitemap_generator 中为 microsoft azure 编写适配器
【发布时间】:2019-01-29 10:51:14
【问题描述】:

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

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

但我不确定仅编辑初始化文件是否会有所帮助。在上面的文章中,Carrierwave 指向 WaveAdapter here,它使用 CarrierWave::Uploader::Base 上传到 CarrierWave 支持的任何服务

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

请帮忙!

【问题讨论】:

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


    【解决方案1】:

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

    将 azure blob gem 添加到您的 Gemfile: gem 'azure-storage-blob'

    创建配置/initializers/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 中创建的容器是“blob”容器,因此该容器不是公开的,但其中的 blob 是公开的。

    然后在 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
    

    应该这样做!

    【讨论】:

    • 哇..太完美了...谢谢你
    • 目前我的站点地图正在 tmp 文件夹中生成。但是当我转到 /sitemap 时。 xml.gz 路径它显示错误''没有路由匹配''......我是否必须编写指向该文件的路由?或者我应该写一条从我的 azure blob 下载文件的路由???
    • 嘿@Vaishnavi 在我的public/robots.txt 中,我有Sitemap: https://[your-azure-address].blob.core.windows.net/sitemaps/sitemap.xml.gz 行,这对所有爬虫AFAIK 来说应该没问题。也有到该位置的重定向路线也没有什么坏处。
    • 在上面的例子中有一个额外的逗号(,)会抛出一个错误“提供的选项无效”.....before inititailize->@storage_access_key............
    猜你喜欢
    • 2020-07-31
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多