[英]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.