使用Ruby aws-sdk跟踪文件到S3的上传进度 [英] Tracking Upload Progress of File to S3 Using Ruby aws-sdk
问题描述
首先,我知道SO中有很多与此问题类似的问题.在过去一周中,即使不是全部,我也阅读了最多的内容.但是我仍然无法为我完成这项工作.
Firstly, I am aware that there are quite a few questions that are similar to this one in SO. I have read most, if not all of them, over the past week. But I still can't make this work for me.
我正在开发Ruby on Rails应用程序,该应用程序允许用户将mp3文件上传到Amazon S3.上传本身可以完美运行,但是进度条将大大改善网站上的用户体验.
I am developing a Ruby on Rails app that allows users to upload mp3 files to Amazon S3. The upload itself works perfectly, but a progress bar would greatly improve user experience on the website.
我正在使用aws-sdk gem,它是亚马逊的官方产品.在上传过程中,我在其文档中到处都有回调的信息,但找不到任何内容.
I am using the aws-sdk gem which is the official one from Amazon. I have looked everywhere in its documentation for callbacks during the upload process, but I couldn't find anything.
一次将文件一次直接上传到S3,因此不需要将其加载到内存中.也无需上传多个文件.
The files are uploaded one at a time directly to S3 so it doesn't need to load it into memory. No multiple file upload necessary either.
我认为我可能需要使用JQuery来完成这项工作,我对此表示满意. 我发现这看起来很有希望: https://github.com/blueimp/jQuery-File-Upload 我什至尝试按照以下示例进行操作: https://github.com/ncri/s3_uploader_example
I figured that I may need to use JQuery to make this work and I am fine with that. I found this that looked very promising: https://github.com/blueimp/jQuery-File-Upload And I even tried following the example here: https://github.com/ncri/s3_uploader_example
但是我只是不能让它对我有用.
But I just could not make it work for me.
aws-sdk的文档还简要描述了带有块的流式上传:
The documentation for aws-sdk also BRIEFLY describes streaming uploads with a block:
obj.write do |buffer, bytes|
# writing fewer than the requested number of bytes to the buffer
# will cause write to stop yielding to the block
end
但这几乎没有帮助.一个如何写入缓冲区"?我尝试了一些直观的选择,这些选择总是会导致超时.而我还要如何基于缓冲来更新浏览器?
But this is barely helpful. How does one "write to the buffer"? I tried a few intuitive options that would always result in timeouts. And how would I even update the browser based on the buffering?
是否有更好或更简单的解决方案?
Is there a better or simpler solution to this?
先谢谢您. 在此方面的任何帮助,我们将不胜感激.
Thank you in advance. I would appreciate any help on this subject.
推荐答案
将块传递给#write时产生的"buffer"对象是StringIO的一个实例.您可以使用#write或#<<写入缓冲区.这是一个使用块形式上传文件的示例.
The "buffer" object yielded when passing a block to #write is an instance of StringIO. You can write to the buffer using #write or #<<. Here is an example that uses the block form to upload a file.
file = File.open('/path/to/file', 'r')
obj = s3.buckets['my-bucket'].objects['object-key']
obj.write(:content_length => file.size) do |buffer, bytes|
buffer.write(file.read(bytes))
# you could do some interesting things here to track progress
end
file.close
这篇关于使用Ruby aws-sdk跟踪文件到S3的上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!