直接上传使用Carrierwave至S3 [英] Direct Uploads to S3 using Carrierwave

查看:271
本文介绍了直接上传使用Carrierwave至S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近转换下面使用回形针Carrierwave上传到Amazon S3这样我就可以利用carrierwave_direct宝石,然后Sidekiq或其他后台处理的宝石。

I've recently converted the below from using Paperclip to Carrierwave uploading to Amazon S3 so I can make use of the carrierwave_direct gem and then Sidekiq or another background processing gem.

class Release < ActiveRecord::Base
  has_many :releases_tracks, :dependent => :destroy
  has_many :tracks, :through => :releases_tracks, :order => "releases_tracks.position DESC"
  accepts_nested_attributes_for :tracks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
  accepts_nested_attributes_for :releases_tracks  
end

class Track < ActiveRecord::Base
  mount_uploader :track, TrackUploader
  has_many :releases_tracks, :dependent => :destroy
  has_many :releases, :through => :releases_tracks 
end

/views/releases/track_upload.html.erb

/views/releases/track_upload.html.erb

<%= form_for(@release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= @release.title %></h3>
    <% index = 0 %>
    <%= f.fields_for :tracks do |builder| %>
    <%= @release.tracks[index].name %>
        <%= f.file_field :track, :class => "file styled", :title => 'Select Track'%>
    <% index += 1 %>
    <% end %>

    <%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>

Carrierwave上传工作,但我无法弄清楚如何获得直接参加工作。部分原因是因为我无法弄清楚如何将建议的形式code:

Carrierwave uploads are working, but I can't figure out how to get the direct part working. Partly because I can't figure out how to incorporate the suggested form code:

<%= direct_upload_form_for @uploader do |f| %>
  <%= f.file_field :track %>
  <%= f.submit %>
<% end %

否则我将在那里我的曲目或释放控制器建议:

Or the where in my track OR release controller I place the suggested:

@uploader = User.new.track
@uploader.success_action_redirect = new_user_url

自述<一href="https://github.com/dwilkie/carrierwave_direct">https://github.com/dwilkie/carrierwave_direct和Railscast <一href="http://railscasts.com/episodes/383-uploading-to-amazon-s3">http://railscasts.com/episodes/383-uploading-to-amazon-s3这两个点对第一上传您的文件,然后创建数据库条目。在我的应用程序数据库的条目已经存在。该Railscast并说这是可能的,但不经过它。所以这是第一个问题。

The readme https://github.com/dwilkie/carrierwave_direct and Railscast http://railscasts.com/episodes/383-uploading-to-amazon-s3 both point towards uploading your file first and then creating your database entry. In my app the db entries already exist. The Railscast does say it's possible but doesn't go through it. So that's the first problem.

第二个是,我需要上传多个文件的时间。在code以上没有做到这一点,但非常缓慢,这当然使我的应用程序pretty的没用,因为它这样做。

The second is that I need to upload more than one file at a time. The code above does achieve that, but very slowly and it of course renders my app pretty useless as it does so.

谁能帮助?大规模提前感谢!

Can anyone help? Massive thanks in advance!

推荐答案

首先,我建议你不要使用carrierwave_direct的,我真的不喜欢这种宝石,原因是多方面的。 其中的一个原因,是因为它在文档据说

First of all I would advise you not to use carrierwave_direct, I really don't like this gem, for many reasons. One of those reasons, is that as it's said in the docs

请注意,这种宝石仅支持单   文件上传。如果你想同时上传多个文件   你将不得不使用JavaScript和Flash上​​传。

Please be aware that this gem only supports single file uploads. If you want to upload multiple files simultaneously you'll have to use a javascript or flash uploader.

但是,如果你想使用它,这里是我想你必须做的:

But if you want to use it, here is what I guess you have to do :

因此​​,首先对

@uploader = User.new.track
@uploader.success_action_redirect = new_user_url

看来你尝试上载的轨道,正如你所说已经创建了模型,我想你正试图上传新的轨道为现有的版本。纠正我,如果我错了。

It seems you are trying to upload tracks, and as you said your models have already been created, I guess your are trying to upload new tracks for an existing release. Correct me if I'm wrong.

所以你应该创建 @uploader VAR中的 #track_upload 方法,你的 ReleasesController

so you should create the @uploader var in the #track_upload method of your ReleasesController.

class ReleasesController
  ...  
  def track_update 
    @uploader = User.new.track
    @uploader.success_action_redirect = new_user_url
  end
  ...
end

然后在相关视图(/views/releases/track_upload.html.erb),您可以使用direct_upload_form

then in the associated view (/views/releases/track_upload.html.erb), you can use the direct_upload_form

<%= direct_upload_form_for @uploader do |f| %>
  <%= f.file_field :track %>
  <%= f.submit %>
<% end %>

的形式,将直接将文件上传到S3,您选择的文件刚过。然后,我不知道到底是怎么回事,但carrierwave_direct应该还给你上传的文件的URL。 我不知道这一点,因为我从来没有去过那么远呢,但这个想法是,你的文件刚刚上传到S3,现在它已被链接到你的模型,因此该文件没有得到'丢失。 因此,也许carrierwave_direct是做自己(我怀疑...)的东西做一些Ajax请求,或其他任何东西。

The form will upload the file directly to s3, just after you selected the file. Then I don't know exactly how, but carrierwave_direct should give you back the url of the uploaded file. I'm not sure about that as I've never been that far with it, but the idea is that your file just got uploaded to s3, now it has to be linked to your model, so the file doesn't get 'lost'. So maybe carrierwave_direct is doing things on its own (I doubt that ...) by doing some ajax requests or anything else.

总之,只要你想上传多个文件,我想你指向的教程中,我最近写了

Anyway as you want to upload more than one file, I'd like to point you to a tutorial I recently wrote

这显示了如何直接上传文件到S3,不carrierwave_direct,但做的事情你自己。这需要一点点的code和时间,但你对所发生的事情更多的控制。

This shows how to upload files directly to s3, without carrierwave_direct, but by doing things on your own. This requires a little bit more code and time, but you have more control about what's happening.

在你的情况,你会希望把我在你看来用我的教程的形式,在/views/releases/track_upload.html.erb视图。 然后,一旦你选择一个文件,成功AJAX请求(由jQueryFileUpload插件发出)会给你上传的文件的URL,这样你就可以把它保存在你的轨道模型(你可能会想发出一个新的AJAX请求你的服务器上创建新的跟踪模式,或在网页上填写的其它形式,就像一个你正在使用的/views/releases/track_upload.html.erb文件,然后在轨道将被保存在提交。 ) 我不知道,我真的很清楚的是,让我知道如果你需要更多的解释。

In your case, you'll want to put the form I'm using in my tutorial in your view, in the /views/releases/track_upload.html.erb view. Then once you'll select a file, the successful AJAX request(emitted by the jQueryFileUpload plugin) will give you the URL of the uploaded file so you can save it in your Track model (you'll probably want to emit a new AJAX request to your server to create the new Track model, or to populate an other form on the page, like the one you were using in the /views/releases/track_upload.html.erb file, and then the tracks will be saved on submit.) I'm not sure I'm really clear about that, let me know if you need more explanations.

和有关的好处是,如果你简单地添加到文件输入,那么真棒的 jQueryFileUpload 插件将发送每个文件到S3的请求,那么你会得到的URL上传的文件,在每个AJAX结果:D

And the good thing about that is that if you simply add multiple to your file input, then the awesome jQueryFileUpload plugin will send a request per file to s3, then you'll get the URL's of the uploaded files in each ajax results :D

和你可以调整的东西添加进度条,之类的东西的jQuery插件,你可以真正创造真棒的东西。

And you can tweak things to add progress bars, and things like that with the jQuery plugin, you can really create awesome things.

希望它会帮助你!

这篇关于直接上传使用Carrierwave至S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆