dimanche 23 août 2015

Uploading FIle from Client to Ruby on Rails Server to Amazon S3

I am working on a website that requires a document tree for file management and have been running into trouble getting files into my Amazon S3 bucket.

My approach is to have the user upload files from the client side into a jQuery plugin I have written. The plugin takes all of the files and packages them up into a Ajax call that gets sent to my internal API that then handles the files.

From here, the files make their way to my S3 uploading logic, but here is where I start having problems. Once the object has come into my server, it's in a split apart form and I am not sure which parts are necessary or not. Ajax did it's magic and broke apart my files into a JSON readable array, but now that its in a new form, I'm not sure how to process it.

Whenever I go to upload the file(s) to the S3 server it says the following.

no implicit conversion of Fixnum into String"

Given I am just throwing the new formatted array of the file into the S3 logic, but I don't know how else to do this. Every other part of my solution so far works in that I can save strings into my S3 bucket no problem. So my question is, how can I take a file object passed into Rails and then pass it into S3? What format do I need?

Below is my code for my API method to upload the files.

def upload_files

    s3 = Aws::S3::Resource.new
    s3_bucket =  s3.bucket(S3_BUCKET_NAME)

    debug_var = @request

    i = 0
    while i < request['file_count'].to_i  do

      # Make an object in your bucket for your upload
      file_id = SecureRandom.uuid

      # Upload the file
      s3_bucket.put_object({
          key: file_id,
          body: @request['file_' + i]
                           })

      i = i + 1
    end

Here is a screen shot of the data that is coming through to the server for the files.

enter image description here




1 commentaire: