简体   繁体   English

将文件从客户端上传到Ruby on Rails Server到Amazon S3

[英]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. 我在一个需要文件树进行文件管理的网站上工作,并且在将文件放入我的Amazon S3存储桶时遇到麻烦。

My approach is to have the user upload files from the client side into a jQuery plugin I have written. 我的方法是让用户从客户端将文件上传到我编写的jQuery插件中。 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. 该插件会提取所有文件,并将它们打包到一个Ajax调用中,该调用将被发送到我的内部API,然后由该内部API处理这些文件。

From here, the files make their way to my S3 uploading logic, but here is where I start having problems. 从这里开始,文件进入我的S3上传逻辑,但是在这里我开始遇到问题。 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. Ajax做到了魔术,将文件分解成JSON可读数组,但是现在它以一种新形式出现,我不确定如何处理它。

Whenever I go to upload the file(s) to the S3 server it says the following. 每当我将文件上传到S3服务器时,都会显示以下内容。

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. 鉴于我只是将文件的新格式化数组放入S3逻辑中,但我不知道该怎么做。 Every other part of my solution so far works in that I can save strings into my S3 bucket no problem. 到目前为止,解决方案的所有其他部分都可以正常运行,因为我可以将字符串保存到S3存储桶中没有问题。 So my question is, how can I take a file object passed into Rails and then pass it into S3? 所以我的问题是,我怎样才能将文件对象传递到Rails中,然后再传递到S3中? What format do I need? 我需要什么格式?

Below is my code for my API method to upload the files. 以下是我用于上传文件的API方法的代码。

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. 这是传递给服务器的文件数据的屏幕快照。

在此处输入图片说明

The error occurs in this line: 该行中发生错误:

# Implicit conversion of int i to a string:
body: @request['file_' + i]

To fix it, do this: 要解决此问题,请执行以下操作:

# Explicit conversion (i.to_s) *is* allowed:
body: @request['file_' + i.to_s]

Or: 要么:

# String interpolation implicitly converts int to string:
body: @request["file_#{i}"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jRecorder和Ruby on Rails将录制的音频文件上传到Amazon S3 - How to upload a recorded audio file to Amazon S3 using jRecorder and Ruby on Rails 将分块文件直接上传到 Amazon s3 - Uploading chunked files directly to Amazon s3 使用Rails 3.2和AJAX(非闪存上传解决方案)将多个文件直接上传到Amazon S3 - Uploading multiple files directly to Amazon S3 using Rails 3.2 and AJAX (non-flash upload solutions) MultiPowUpload(Flash文件上传组件)在上传到Amazon S3时给出错误#2049 - MultiPowUpload (flash file upload component) giving Error #2049 while uploading to Amazon S3 使用SDK和Uploadify将文件上载到Amazon S3时显示的上载进度不正确 - Incorrect upload progress shown when uploading file to Amazon S3 using SDK and Uploadify 使用jQuery fileupload插件直接上传到Amazon S3 - Uploading directly to Amazon S3 with jQuery fileupload plugin 使用上传到Amazon S3的JQuery文件在客户端调整图像大小 - Resizing image at client-side using JQuery file upload to amazon S3 Rails载波和JQuery Uploader到Amazon S3 - Rails Carrier Wave and JQuery Uploader to Amazon S3 从ASP.NET MVC将Ajax文件上传到Amazon s3 - Ajax file upload to Amazon s3 from ASP.NET MVC Shrine + JQuery File Upload仅将缓存文件上传到S3 - Shrine + JQuery File Upload only uploading cache file to S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM