Rails direct to S3 upload using aws-sdk gem and jQuery-File-Upload on heroku

前端 未结 2 1885
日久生厌
日久生厌 2020-12-21 01:09

I\'m trying to achieve direct to Amazon S3 upload in Rails using jQuery-File-Upload and the aws-sdk gem, and following heroku\'s direct to S3 upload instructions. This is th

2条回答
  •  甜味超标
    2020-12-21 01:55

    This code uses aws-sdk-v2 for ruby, it's been submitted to Heroku to update their documentation on s3 direct uploads (https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails#pre-signed-post)

    Some aspects of this are not necessary, i.e. storing your own photo object in the DB after uploading to s3. And the code is obviously not as good as it could be, this was just to get it working. I'm happy to hear any suggestions to improve it or know if something here isn't working for you.

    The Sidekiq job has a method do_work which is a class I use defined in BaseJob to give me some additional metrics and info you can't get without SidekiqPro, so you would need to change this to "perform" if you're not doing anything special with the Sidekiq perform class.

    #ROUTE CODE
    post 'create_photo' => 'users#create_photo'
    
    #CONTROLLER CODE
    
    #intial view, might be update or another method in your case
    def new
      @user = current_user.find params[:user_id]            
    
      @s3_direct_post = Aws::S3::Bucket.new(name: ENV['aws_bucket']).presigned_post(key: "musea_upload/harrisjb/${filename}", success_action_status: "201", acl: "public-read") 
    end
    
    #called from ajax method in code below
    # I store a photograph object with an id and s3 url in my DB
    # this code calls a sidekiq job in the user model to create 
    # the photo object in the DB after the photo is uploaded directly to s3
    # not essential, but helpful to reference uploaded photos
    
    def create_photo                                                              
      @user = current_user.find params[:user_id]            
      options = {}                                                                
      options['user_id'] = params[:user_id]                                     
      options['key'] = params[:key]                                               
      options['url'] = params[:url]                                               
      @user.create_photograph_from_s3(options)                                   
    
      respond_to do |format|                                                      
        format.js { }                                                             
       end                                                                         
    end  
    
    #MODEL CODE
    def create_photograph_from_s3(options)
      CreatePhotographJob.perform_async(options)
    end
    
    #JOB CODE
      def do_work(options)                                                          
        user_id = options['user_id']                                              
        user = User.find_by(id: user_id)                                                
        Rails.logger.info "CREATE PHOTOGRAPH JOB FOR USER #{user_id}"             
        url = options['url'].gsub("//h", "h")                                       
        user.create_photograph_from_file(url)                                
        Rails.logger.info "PHOTOGRAPH CREATED"                                      
      end 
    
    
    #VIEW CODE
    <%= form_for(:user_avatar, :remote => true, :url => p_user_avatar_upload_to_s3_path(user_id: @user.id), html: { id: "uploader", class: "uploader white-bg" }) do |f| %>                                                                                
      
      <%= f.file_field :photo, multiple: true, style: 'margin-top: 20px; height: 5em; width: 100%; padding-top: 5%; padding-left: 20%;', class: 'form-group form-control light-gray-bg' %>                                       
    <% end %>                         
    
    # JS -- if you want this in your view use the content_for
    # or you can store the functions in application.js or wherever you want
    
    <% content_for :javascript do %>
        
    <% end %>
    

提交回复
热议问题