Re: Paperclip s3 copy image

Walter Lee Davis
2013-08-07T13:36:45Z

On Aug 7, 2013, at 5:59 AM, Avi wrote:

> 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

>
> Thanks,
> Avi
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/63dfa195-e9aa-4884-9e44-d50958426641%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/372BF8FA-E220-4E9B-A539-E56D710DBF9B%40wdstudio.com.
For more options, visit https://groups.google.com/groups/opt_out.