在 sitemap_generator 中为 Microsoft Azure 编写适配器
Write adapter for microsoft azure in sitemap_generator
我正在使用 sitemap_generator 在我的 RoR 中生成站点地图 project.Everything 工作正常,直到 now.I 我在 Heroku 上托管我的项目,它不允许写入本地 filesystem.I 仍然需要一些写入权限,因为站点地图文件需要在上传前写出。但是我必须使用 Microsoft azure 来存储我在 sitemap_generator 中列出的 sitemap.The 适配器不包括 azure.Could 有人指出我为 azure 编写适配器的正确方向。
参考this文章中的"Configure carrierwave",我对我的代码做了一些修改。
但我不确定仅编辑初始化文件是否会 help.In 上面的文章 Carrierwave points to WaveAdapter here 使用 CarrierWave::Uploader::Base 上传到支持的任何服务载波
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
请帮忙!
我从 the S3 adapter and from Azure's ruby example
复制了我的设置
将 azure blob gem 添加到您的 Gemfile 中:
gem 'azure-storage-blob'
创建 config/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' 容器,因此容器不是 public 但其中的 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
应该可以!
我正在使用 sitemap_generator 在我的 RoR 中生成站点地图 project.Everything 工作正常,直到 now.I 我在 Heroku 上托管我的项目,它不允许写入本地 filesystem.I 仍然需要一些写入权限,因为站点地图文件需要在上传前写出。但是我必须使用 Microsoft azure 来存储我在 sitemap_generator 中列出的 sitemap.The 适配器不包括 azure.Could 有人指出我为 azure 编写适配器的正确方向。
参考this文章中的"Configure carrierwave",我对我的代码做了一些修改。
但我不确定仅编辑初始化文件是否会 help.In 上面的文章 Carrierwave points to WaveAdapter here 使用 CarrierWave::Uploader::Base 上传到支持的任何服务载波
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
请帮忙!
我从 the S3 adapter and from Azure's ruby example
复制了我的设置将 azure blob gem 添加到您的 Gemfile 中:
gem 'azure-storage-blob'
创建 config/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' 容器,因此容器不是 public 但其中的 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
应该可以!