【问题标题】:Paperclip and Amazon S3 how to do paths?Paperclip 和 Amazon S3 怎么做路径?
【发布时间】:2011-09-19 19:30:09
【问题描述】:

使用 Amazon S3 时如何使用回形针创建路径?

我的存储桶上的目录是:

/image/:id/:filename

我的模特:

  has_attached_file :image,
    :storage => :s3,
    :bucket => 'mybucket',
    :s3_credentials => {
      :access_key_id => ENV['S3_KEY'],
      :secret_access_key => ENV['S3_SECRET']
    }

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 paperclip


    【解决方案1】:

    试试这个:

      has_attached_file :image,
        :storage => :s3,
        :bucket => 'mybucket',
        :path => "/image/:id/:filename",
        :s3_credentials => {
          :access_key_id => ENV['S3_KEY'],
          :secret_access_key => ENV['S3_SECRET']
        }
    

    【讨论】:

      【解决方案2】:

      几个月前我写了一篇关于它的帖子。 我还写了关于如何从类中添加属性的文章,例如不使用 id(我不喜欢它)而是使用令牌。

      Read the post here...

      基础知识:

      获取带有 id 的路径

      has_attached_file :avatar,
        :styles =>
        {
          :tiny => "48x48>",
          :preview => "175x175>",
          :large => "300x300>",
          :huge => "500x500>"
        },
        :storage => :s3,
        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
        :path => ":class/:attachment/:id/:style.:extension",
        :bucket => 'lopsum',
        :default_url => "/images/photo01.jpg"
      

      而且,如果您想将其更改为其他内容...

      has_attached_file :avatar,
        :styles =>
        {
          :tiny => "48x48>",
          :preview => "175x175>",
          :large => "300x300>",
          :huge => "500x500>"
        },
        :storage => :s3,
        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
        :path => ":class/:attachment/:token/:style.:extension",
        :bucket => 'lopsum',
        :default_url => "/images/photo01.jpg"
      

      在初始化器中

      Paperclip.interpolates :token do |attachment, style|
        attachment.instance.token
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多