Paperclip and Amazon S3 how to do paths?

为君一笑 提交于 2019-12-21 03:42:46

问题


How do I create paths with paperclip when using Amazon S3?

My directory on my bucket is:

/image/:id/:filename

My model:

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

回答1:


Try this:

  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:


I wrote a post about it a few months back. I also wrote about how you can add properties from the class, for example not using an id (I don't like it) and using a token instead.

Read the post here...

The basics:

to get a path with an 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"

and, if you want to change it to something else...

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"

and in an initializer

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


来源:https://stackoverflow.com/questions/7476255/paperclip-and-amazon-s3-how-to-do-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!