【问题标题】:CarrierWave With Cloudfront Generating wrong URL带有 Cloudfront 的 CarrierWave 生成​​错误的 URL
【发布时间】:2016-12-10 01:48:32
【问题描述】:

您好,我正在使用 Cloudfront 与 CarrierWave 和 S3。出于某种原因,在我的应用程序中,当使用 image_tag 时,我的云端路径得到了非常奇怪的 URL,而且我不知道我哪里出错了。这是我的载波配置文件

CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'                        # required

config.fog_credentials = {
provider:              'AWS',                        # required
aws_access_key_id:     'accesskey',                        #   required
aws_secret_access_key: 'secretaccess key',                          # required
region:                'us-east-1',                  # optional, defaults to 'us-east-1'
}

config.fog_directory  = 'bwautosales'                          # required
# config.asset_host = 'randomletters.cloudfront.net'
config.asset_host = 'randomletters.cloudfront.net'
config.fog_public = true
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } #  optional, defaults to {}
 end

还有我的 production.rb 文件

 config.action_controller.asset_host = 'randomletters.cloudfront.net'

但在我的观点中,当我做类似的事情时

 <%= link_to image_tag(car.images[0], class: "img-responsive right-block", id: index), car %>

我明白了 -

src = https://randomletters.cloudfront.net/images/randomletters.cloudfront.net/uploads/car/images/28/car.png"

我确信这是一个简单的解决方法,但不知道我哪里出错了。任何帮助将不胜感激!

图片上传器 `

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::MiniMagick

storage :fog

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def extension_whitelist
 %w(jpg jpeg gif png)
end

end

`

【问题讨论】:

  • 也许你应该改用这个:car.images[0].file.url
  • 感谢您的建议,不幸的是,这似乎没有什么不同。
  • 如果我从生产 .rb 中删除该行,它将变为 /images/randomletters.cloudfront.net/uploads/car/images/28/car.png"
  • 我们可以看到您使用的上传文件吗?就我而言,我没有将 config.asset_host = 'randomletters.cloudfront.net' 放在主文件 carrier wave.rb 中,而是放在上传器中。
  • 用上传者更新了我上面的帖子

标签: ruby-on-rails amazon-s3 asset-pipeline carrierwave amazon-cloudfront


【解决方案1】:

在您的 Carrierwave 初始化程序中更改行,
config.asset_host = 'randomletters.cloudfront.net'config.asset_host = 'http://randomletters.cloudfront.net' 以获取所需的云端 URL。

您还可以从 Production.rb 中删除行 config.action_controller.asset_host = 'randomletters.cloudfront.net',因为carrierwave 本身会从云端网址获取。

CarrierWave.configure do |config|
    config.fog_provider = 'fog/aws'                        

    config.fog_credentials = {
    --------------
    --------------
    }

    config.fog_directory  = 'bwautosales'                          
    config.asset_host = 'http://randomletters.cloudfront.net' # please check the change in this line.
    config.fog_public = true
    config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } #  optional, defaults to {}
end

【讨论】:

    【解决方案2】:

    只是为了增加 Sravan 回答的可信度,我遇到了同样的问题,当我按照他的指示进行操作时,它开始正常工作。如果您从

    中省略了 http://
    config.asset_host = <>
    

    它会感到困惑,并尝试对您网站的域名使用不同的路径。我有同样的问题,但添加 http:// 到网址的开头修复它。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2017-08-18
      • 2019-09-28
      • 2016-12-09
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 2016-04-26
      相关资源
      最近更新 更多