Direct Uploads to S3 using Carrierwave

前端 未结 1 1836
情话喂你
情话喂你 2021-02-09 08:27

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 backgro

1条回答
  •  北海茫月
    2021-02-09 08:55

    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

    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 :

    So first about the

    @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.

    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
    

    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 %>
    

    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

    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.

    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.

    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

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

    Hope it'll help you !

    0 讨论(0)
提交回复
热议问题