2011-09-19 101 views
12

如何在使用Amazon S3時使用回形針創建路徑?回形針和亞馬遜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'] 
    } 

回答

18

試試這個:

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'] 
    } 
10

我寫了一篇關於它在幾個月前。 我也寫過關於如何從類中添加屬性,例如不使用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