25

I have video model with the following definition:

class Video
  require 'carrierwave/orm/activerecord'
  mount_uploader :attachment, VideoUploader
  mount_uploader :attachment_thumbnail, VideoThumbnailUploader
  ...
end

When I upload a video file. It also sends the file to our encoding service Zencoder, which encodes the video file and creates a thumbnail for it.

Normally, I could do something like @video.attachment.url, which will return the path of the video file. I'd like to do the same thing with the thumbnail. i.e. @video.attachment_thumbnail.url

However, since the attachment is created by our encoding service, which also uploads it to a specified S3 bucket. How do I assign the attachment to the attachment_thumbnail column for the record?

Can I simply do something like:

@video.update_attributes(
  :attachment_thumbnail => 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'
)

Is it possible to assign files like this to Carrierwave?

Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215
  • You watch railscasts, don't you? ;) Did you use paperclip before? Which one do you prefer? – Robin Feb 15 '11 at 18:15
  • 1
    Hi Robin, tried Paperclip. I prefer Carrierwave. Just a personal preference :-) – Christian Fazzini Feb 15 '11 at 18:28
  • 1
    Check out the answer here. update_column skips callbacks allowing you to update the attribute without having CarrierWave re-upload the image. http://stackoverflow.com/questions/16968658/how-do-i-add-files-already-stored-on-s3-to-carrierwave-backed-by-same-datastore?rq=1 – pejmanjohn Aug 22 '13 at 22:41

4 Answers4

50

You can do the following:

@video.remote_attachment_thumbnail_url = 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'

But that will cause Carrierwave to download + reprocess the file rather than just make it the thumbnail. If you're not going to use Carrierwave's processing, then it might make more sense to just store the URL to the thumbnail on the model rather than even using Carrierwave.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
ctide
  • 5,217
  • 1
  • 29
  • 26
  • 1
    Why would "@video.remote_attachment_thumnail_url" cause Carrierwave to download + reprocess the file? – Christian Fazzini Feb 15 '11 at 18:28
  • 1
    You're just telling carrierwave to grab the file from there vs. having the file uploaded. It's generally used in forms for the user to enter a URL to an image that they'd like to upload. – ctide Feb 15 '11 at 19:58
  • ctide, I just tried: @video.update_attributes(:attachment_thumbnail => 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png') and I get: CarrierWave::FormNotMultipart: CarrierWave::FormNotMultipart. I guess you can't do @video.remote_attachment_thumnail_url = 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png', which is merely the same thing. Or am I missing something? – Christian Fazzini Feb 15 '11 at 21:30
  • 5
    It's not the same thing, remote_[uploader]_url is an instance method that tells Carrierwave to download the file from that URL. See: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/mount.rb line 87 Also, it's not an attribute, so you can't set it via update_attributes. – ctide Feb 15 '11 at 22:22
  • 2
    Is it possible to assign the remote file to CarrierWave only? This means not for it to download or reprocess, just assign. So that I may utilize the helper as such: @video_attachment_thumbnail.url – Christian Fazzini Feb 18 '11 at 05:58
  • No, Carrierwave doesn't support that because you're trying to exclude everything Carrierwave does, while still using Carrierwave. If you just want to store a URL to an image, store a URL to an image in your database, it'll be much easier. – ctide Feb 18 '11 at 06:44
  • Ok that makes sense. On another note, I have tried the following: >> Video.last.remote_attachment_thumbnail_url = 'http://bucket_name.s3.amazonaws.com/uploads/users/3/video/29/thumbnail_0000.png'. In the console, it returns: "http://bucket_name.s3.amazonaws.com/uploads/users/3/video/29/thumbnail_0000.png", but when I type Video.last.attachment_thumbnail.url, I get nil. Am I doing something wrong? – Christian Fazzini Feb 18 '11 at 08:31
  • do video.last_attachment_thumbnail_url and make a field in your DB called last_attachment_thumbnail_url in the video table, and store it there. By adding more separators (periods) you're just going to confuse activerecord! – ctide Feb 18 '11 at 08:41
  • Hi ctide. I am trying to assign a remote url to my uploader, not a url string to my db column – Christian Fazzini Feb 18 '11 at 08:52
  • spent hours trying to figure this out today, thanks for the tip re: remote_xxx_url – stephenmurdoch Aug 22 '11 at 18:42
  • it seems now need to update the 'uploader' attribute manually(Rails 3.1.3 carrierwave (0.5.7)),such as "result = @video.remote_attachment_thumbnail_url = 'http://some_remote_img.png' and @video.update_attribute(:attachment_thumbnail,result) " then it will works fine – Mike Li Dec 28 '11 at 04:33
11

This worked for me, with CarrierWave 0.5.8

model.update_attributes(:remote_uploader_url => "http://path/to/image.jpg")

Of course, you need to set remote_uploader_url to be attr_accessible for this.

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
asif
  • 111
  • 1
  • 2
0

I was looking for this as well.

The blocking point in the zencoder case would be that Carrierwave doesn't track different different file type versions for the original file. It only references the original file.

So having the original file as an .mp4 a a thumbnail version as a .png doesn't work. While you can have an 'image.png' and also track 'thumb_png_image.png', you can't also create a 'thumb_jpg_image.jpg' for the same file.

Otherwise you could create a dummy version and using conditional versioning tell CW not to process it. Since CW would create the dummy version anyway but not upload it, you could have it reference a path matching the file returned by Zencoder. But oh well...

tomek
  • 758
  • 1
  • 8
  • 12
0

At the end of this episode (7:35), Ryan Bates adds a remote_image_url in a file form upload:

http://railscasts.com/episodes/253-carrierwave-file-uploads

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236