【问题标题】:Paperclip URL helper providing wrong URL when using S3回形针 URL 帮助程序在使用 S3 时提供错误的 URL
【发布时间】:2014-05-13 04:18:40
【问题描述】:

我正在使用带有 Rails 的 Paperclip gem 来上传图像,当我使用带有 gem 的 img 标签助手时,它会输出错误的 URL。这是模型代码:

class Org < ActiveRecord::Base
    has_many :event
    has_many :solookup
    belongs_to :student

    has_attached_file :org_pic, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/assets/clubhouse.jpg", :storage => :s3, :s3_credentials => Proc.new{|a| a.instance.s3_credentials}, :s3_host_name => "branchapp.s3.amazonaws.com"
    validates_attachment_content_type :org_pic, :content_type => /\Aimage\/.*\Z/

    def s3_credentials
        {:bucket => "branchapp", :access_key_id => "hidden", :secret_access_key => "hidden"}
    end
end

上传效果很好,但是输出的url是这样的:

http://branchapp.s3.amazonaws.com/branchapp/orgs/org_pics/000/000/002/original/IMG_0539.JPG?1396413590

我不知道如何在 .com 之后删除 /branchapp。如果将其删除,则链接可以正常工作。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    has_attached_file 中,您需要覆盖url 选项。默认情况下,url 使用":s3_path_url",它将存储桶放在 url 中,就像你看到的那样。您需要改用":s3_domain_url"

    添加:

    :url =&gt; ":s3_domain_url"

    到您的has_attached_file 选项。

    注意:":s3_domain_url" 应在主机前加上存储桶名称,因此您可能需要从 s3_host_name 选项中删除 branchapp。 (:s3_host_name =&gt; "s3.amazonaws.com")

    【讨论】:

      【解决方案2】:

      为了给您更多信息,您不妨查看:s3 documenation for Paperclip。我们使用这个设置(效果很好):

      #config/environments/production.rb
      config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com" 
      
      Paperclip::Attachment.default_options.merge!({
         storage: :s3,
         s3_host_name: 's3-eu-west-1.amazonaws.com',
         s3_credentials: {
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
         },
         bucket: ENV['S3_BUCKET_NAME']
      })
      

      这让我们可以调用:

      #app/models/image.rb
      Class Image < ActiveRecord::Base
          has_attached_file :image
      end
      

      【讨论】:

        猜你喜欢
        • 2018-04-09
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 2012-05-18
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        相关资源
        最近更新 更多