Ruby-on-rails 如何将远程文件分配给 Carrierwave?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5007575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign a remote file to Carrierwave?
提问by Christian Fazzini
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.
当我上传视频文件时。它还将文件发送到我们的编码服务 Zencoder,后者对视频文件进行编码并为其创建缩略图。
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
通常,我可以执行类似@video.attachment.url 的操作,它会返回视频文件的路径。我想对缩略图做同样的事情。即@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?
但是,由于附件是由我们的编码服务创建的,它也会将其上传到指定的 S3 存储桶。如何将附件分配给记录的附件缩略图列?
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?
是否可以将这样的文件分配给 Carrierwave?
回答by ctide
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.
但这将导致 Carrierwave 下载并重新处理文件,而不仅仅是将其设为缩略图。如果您不打算使用 Carrierwave 的处理,那么将 URL 存储到模型上的缩略图可能比使用 Carrierwave 更有意义。
回答by asif
This worked for me, with CarrierWave 0.5.8
这对我有用,CarrierWave 0.5.8
model.update_attributes(:remote_uploader_url => "http://path/to/image.jpg")
Of course, you need to set remote_uploader_urlto be attr_accessiblefor this.
当然,你需要设置remote_uploader_url成为attr_accessible了这一点。
回答by tomek
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.
zencoder 案例中的阻塞点是 Carrierwave 不会跟踪原始文件的不同文件类型版本。它只引用原始文件。
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.
因此,将原始文件作为 .mp4 aa 缩略图版本作为 .png 是行不通的。虽然您可以拥有“image.png”并跟踪“thumb_png_image.png”,但您不能同时为同一文件创建“thumb_jpg_image.jpg”。
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...
否则,您可以创建一个虚拟版本并使用条件版本控制告诉 CW 不要处理它。由于 CW 无论如何都会创建虚拟版本但不会上传它,因此您可以让它引用与 Zencoder 返回的文件匹配的路径。但是哦,好吧...
回答by Benjamin Crouzier
At the end of this episode (7:35), Ryan Bates adds a remote_image_url in a file form upload:
在这一集的结尾(7:35),Ryan Bates 在文件格式上传中添加了一个 remote_image_url:

