Paperclip s3 copy image

Hello All,

I have an image in paperclip s3 amazon bucket.

I want to copy that image & save it into another model.

But I also want to create a new image with another name.

how can I achieve it?

Example:-

I have an image name a.png in s3 bucket in a model named “sample.rb”.

I have to take the same image(a.png) and save it as another image in paperclip s3 bucket with different name(b.png).

Any suggestions?

Thanks,

Avi

Hello All,

I have an image in paperclip s3 amazon bucket.
I want to copy that image & save it into another model.
But I also want to create a new image with another name.
how can I achieve it?

Example:-

I have an image name a.png in s3 bucket in a model named "sample.rb".
I have to take the same image(a.png) and save it as another image in paperclip s3 bucket with different name(b.png).

Any suggestions?

I haven't tried this (but I will need to this week). What I would do is spin up a new object to contain the original image, and then use open-uri to load the file data from the original into it. I found this bit of code on SO; I used it to transfer file data from CarrierWave to Paperclip already, so it's been tested to work:

(my has_attached_file is called blob, that's what blob means below)

  def blob_url(url)
    begin
      require "open-uri"
      f = open(url)
      def f.original_filename ; base_uri.path.split('/').last ; end

      self.blob = f
    rescue OpenURI::HTTPError => e
      raise(e) unless e.message == "404 Not Found"
    ensure
      f.close if f
    end
  end

So then you could do something like this:

@original = YourModel.find(original_id)
@copy = YourModel.new
@copy.blob_url(@original.blob.expiring_url)
@copy.save

That will fetch the original file data, and store it again in a new path for the new object. It's not particularly elegant, but given that you definitely don't want to just reference the same Paperclip storage object from two different AR objects (because one of them could be deleted, nuking the files for both) I believe it's the way forward here.

Walter