Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails paperclip S3 with dynamic bucket name

I'm using paperclip to upload my documents to Amazon S3. I would like to automatically create a bucket with the ID of my projects when I upload a new document.

Therefore, in my controller, I have this:

 def new
    @pmdocument = Pmdocument.new
    @pmdocument.projectmilestone_id=params[:projectmilestone_id]

where projectmilestone_id is the foreign_key to my project (to be used as my bucket name)

My model is like this:

class Pmdocument < ActiveRecord::Base
  belongs_to :projectmilestone
  attr_accessible :id, :name, :description, :projectmilestone_id, :pmdoc, :projectmilestone_attributes
  attr_protected :pmdoc_content_type, :pmdoc_size
  accepts_nested_attributes_for :projectmilestone, :allow_destroy => false
  has_attached_file :pmdoc,
    :storage => :s3,
    :bucket => self.projectmilestone_id.to_s,
    :s3_credentials => File.join(Rails.root, 'config', 's3.yml')

When I load the page, I get this error: undefined method `projectmilestone_id' for #

I checked my controller and the projectmilestone_id field is correctly loaded there.

I tried to change the bucket line to :bucket => self.name and then the error is gone.

The model works ok because projectmilestone_id is correctly stored in the db.

My guess is that it could be linked to the accessible attributes but it seems to be ok too.

What's wrong? Many thanks!!!


I really dont get it:

I decided not to change my bucket anymore (bad idea anyway as the name need to be unique for all S3) but to change my path instead.

This is the code:

:path => proc { |attachment| "#{attachment.istance.projectname}/:attachment/:id/:basename.:extension" },

The first folder with my project name is not created. If I replace projectname by name, or even description (another field of pmdocuments), it works, but not with projectname. Of course, I checked that projectname is correctly populated. The reason is elsewhere.

Any clue?

like image 850
ndemoreau Avatar asked May 12 '11 18:05

ndemoreau


1 Answers

The has_attached_file method is executed in the context of the class (when the file is loaded), not in the context of a record instance where you could use attributes and other instance method. self.name works indeed, but it returns the name of the class ("Pmdocument"), not the name of a record.

But Paperclip is kind enough to allow what you want. The documentation on the S3 storage says:

You can define the bucket as a Proc if you want to determine it’s name at runtime. Paperclip will call that Proc with attachment as the only argument.

In your case it would be something like this:

has_attached_file :pmdoc,
  :storage => :s3,
  :bucket => proc { |attachment| attachment.instance.projectmilestone_id.to_s },
  :s3_credentials => File.join(Rails.root, 'config', 's3.yml')

Now you pass a Proc to has_attached_file. The content of the block is not evaluated while your class is loaded, but later when it's needed. Paperclip then calls the block with the attachment as argument and uses the returned value as bucket name.

Edit:

Unfortunately, this block is run when the file is assigned, not when the record is saved. So all your attributes may not be set yet (the order of the assignment of the attributes when you do Pmdocument.new(params[:pmdocument]) is undetermined). I'd like Paperclip to work another way, but in the meantime I see 2 options:

You can remove the file from the params in the controller and set it when everything else is ready:

pmdoc = params[:pmdocument].delete(:pmdoc)
@pmdocument = Pmdocument.new(params[:pmdocument])
@pmdocument.pmdoc = pmdoc

Or you can delay Paperclip post-processing by disabling it with before_post_process (see the Events section of the README) and running it in an after_save callback.

like image 88
Michaël Witrant Avatar answered Oct 01 '22 06:10

Michaël Witrant