thoughtbot/paperclip — Issue #647: Copying attachments should perform S3 copy operation
Author: digitalhobbit
Created: 2011-11-20 04:03:28+00:00
State: open

We frequently need to clone ActiveRecord models that have Paperclip attachments, which are stored on S3. Our clone methods usually look something like this:

def clone
cloned_model = ... # whatever it takes to clone this object
cloned_model.image = self.image
cloned_model
end

Paperclip handles this in its #assign method, by first downloading the S3 object to a temp file, then re-uploading it for the cloned model. This is causing significant performance issues for us, as often need to clone object hierarchies that have many Paperclip attachments.

Ideally, this would leverage remote S3 copy operations, which are supported in recent versions of aws-s3 (S3Object.copy).

For now, I was able to get this working by monkey patching Paperclip. Essentially, I changed #assign to copy the basic metadata only, and then store the original attachment in an instance variable. I then added a flush_copies method that (analogous to flush_writes) gets called when the parent object is saved. If it finds the instance variable containing the original attachment, it performs a S3Object.copy operation.

While this is still not as fast as I'd like (I guess even the S3 copy operations are somewhat slow), it's a significant improvement.

Has this been considered already? Before turning my monkey patch into a clean enhancement and submitting a pull request, I'd like to gauge whether there would be interest in this. Any thoughts? Concerns?