【问题标题】:Fog directory appends local system path with amazon url, when hosting images to s3 with paperclip gem当使用回形针 gem 将图像托管到 s3 时,雾目录会在本地系统路径中附加亚马逊 url
【发布时间】:2014-05-30 21:16:33
【问题描述】:

我正在尝试托管我的网站,该网站在带有雾宝石的 aws-s3 上具有回形针附件图像。但是我的雾目录采用了错误的路径,但它附加了我的本地文件系统路径。

这是我的代码

class RealEstate < ActiveRecord::Base
  has_attached_file :image,
                    :storage => :fog,
                    :fog_credentials => "#{Rails.root}/config/s3.yml",
                    :fog_directory => "#{Rails.root}/config/fog.yml"
end

如果我只在这里定义存储桶名称,那么它会起作用,但它不能为不同的环境使用不同的存储桶

:fog_directory => "development_bucket_name" #works fine but cant use different bucket for different env

这是我的fog.yml

development:
  fog_directory: development_bucket
staging:
  fog_directory: testing_bucket
production:
  fog_directory: production_bucket

它创建的路径是:

https://s3.amazonaws.com//home/Desktop//config/fog.yml/real_estate/image/000/000/185/original/4bec7.png?1396429186

【问题讨论】:

  • 是的,当我在模型中定义存储桶名称时,我做了 Greame,一切正常。 :fog_directory => "development_bucket_name",但是我不能为不同的环境定义不同的桶。
  • fog_directory 到底是什么?图片上传到哪里?存储?在测试环境中......应该是什么?它需要存在吗?
  • fog_credentials中asset_host和fog_directory有什么区别?

标签: ruby-on-rails ruby amazon-s3 paperclip fog


【解决方案1】:

Paperclip 不知道您传递的字符串是配置文件的路径 - 它需要实际的存储桶名称。

您需要解析 yaml 文件并从中提取存储桶名称。例如

directories = YAML.load(File.read(Rails.root.join('config', 'fog.yml')))
has_attached_file :image,
                  :storage => :fog,
                  :fog_credentials => "#{Rails.root}/config/s3.yml",
                  :fog_directory => directories[Rails.env]['fog_directory']

【讨论】:

    猜你喜欢
    • 2016-07-28
    • 1970-01-01
    • 2011-04-15
    • 2012-10-11
    • 2014-08-01
    • 2013-10-19
    • 2012-07-07
    • 2016-01-11
    • 2017-03-22
    相关资源
    最近更新 更多