У меня была такая же проблема, и я слишком долго искал ответ! Получается, что при установке fog_public = false CarrierWave будет игнорировать config.asset_host. Вы можете продемонстрировать это, установив config.fog_public = true: ваши URL-адреса теперь будут URL-адресами CloudFront, а не URL-адресами S3. Ранее этот вопрос поднимался:
https://github.com/carrierwaveuploader/carrierwave/issues/1158 https://github.com/carrierwaveuploader/carrierwave/issues/1215
В недавнем проекте мне нравилось использовать CarrierWave для обработки загрузок в S3, но я хотел, чтобы он возвращал подписанный URL-адрес CloudFront при использовании Model.attribute_url. Я придумал следующий (по общему признанию, уродливый) обходной путь, который, как я надеюсь, может принести пользу другим или улучшить его:
Добавьте гем 'cloudfront-signer' в свой проект и настройте его. по инструкции. Затем добавьте следующее переопределение /lib/carrierwave/uploader/url.rb в новый файл в config/initializers (обратите внимание на несколько вставок AWS:: CF::Signer.sign_url):
module CarrierWave
module Uploader
module Url
extend ActiveSupport::Concern
include CarrierWave::Uploader::Configuration
include CarrierWave::Utilities::Uri
##
# === Parameters
#
# [Hash] optional, the query params (only AWS)
#
# === Returns
#
# [String] the location where this file is accessible via a url
#
def url(options = {})
if file.respond_to?(:url) and not file.url.blank?
file.method(:url).arity == 0 ? AWS::CF::Signer.sign_url(file.url) : AWS::CF::Signer.sign_url(file.url(options))
elsif file.respond_to?(:path)
path = encode_path(file.path.gsub(File.expand_path(root), ''))
if host = asset_host
if host.respond_to? :call
AWS::CF::Signer.sign_url("#{host.call(file)}#{path}")
else
AWS::CF::Signer.sign_url("#{host}#{path}")
end
else
AWS::CF::Signer.sign_url((base_path || "") + path)
end
end
end
end # Url
end # Uploader
end # CarrierWave
Затем переопределите /lib/carrierwave/storage/fog.rb, добавив в конец того же файла следующее:
require "fog"
module CarrierWave
module Storage
class Fog < Abstract
class File
include CarrierWave::Utilities::Uri
def url
# Delete 'if statement' related to fog_public
public_url
end
end
end
end
end
Наконец, в config/initializers/carrierwave.rb:
config.asset_host = "http://d12345678.cloudfront.net"
config.fog_public = ложь
Вот и все. Теперь вы можете использовать Model.attribute_url, и он вернет подписанный URL-адрес CloudFront для частного файла, загруженного CarrierWave в вашу корзину S3.
person
Rob
schedule
30.03.2014