Kevin Sylvestre
Kevin Sylvestre

Reputation: 38012

Paperclip Processor Operate on S3

I am trying to create a custom Paperclip::Processor that integrates with an external web service (the processor will call the web service whenever a new file is uploaded). The external service needs the file to be present in S3 and will handle uploading the processed versions to S3 automatically.

Can this be done using a custom Paperclip::Processor or should it be done with an ActiveRecord callback? If a Paperclip::Processor will work, what is the best way to trigger the upload? Ideally I'd like to do a processor, but the requirement is that the original file MUST be uploaded to S3 first. I have taken a look at using after_create calls, but it sometimes seems to conflict with the after_create used in paperclip. Thanks.

Upvotes: 3

Views: 1055

Answers (2)

pushmatrix
pushmatrix

Reputation: 746

I dealt with a similar issue recently, and it was with the after_save callback. I managed to fix my problem by defining paperclip (has_attached_file ...) after I defined my after_save. This way, paperclip's callback will fire after mine.

Upvotes: 2

Ariejan
Ariejan

Reputation: 11069

You can do this to create a local copy of the file. If it's on S3 it will be downloaded.

tmp_file = @model.attached_file.to_file => TempFile<...>

You can then do your operations on this TempFile. When you're don:

@model.attached_file = tmp_file
@model.save

Edit: misread your question. You can use the before_post_process and after_post_process hooks to perform tasks before or after the file was processed.

class Model < AR::Base
  has_attached_file :avatar

  after_post_process :ping_webservice

  private

  def ping_webservice
    # Do your magic here.
  end
end

Upvotes: 3

Related Questions