2011-09-19 22 views
12

¿Cómo creo rutas con clip cuando uso Amazon S3?Paperclip y Amazon S3 ¿cómo hacer rutas?

Mi directorio en mi cubo es:

/image/:id/:filename 

Mi modelo:

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

Respuesta

18

Prueba esto:

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

escribí un post sobre ello hace unos meses. También escribí sobre cómo puede agregar propiedades de la clase, por ejemplo, no usar una identificación (no me gusta) y usar un token.

Read the post here...

Los fundamentos:

para obtener una ruta con un 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" 

y, si se desea cambiar a otra cosa ...

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" 

y en un inicializador

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