[英]How can I pipe a rack file upload directly to S3?
Is it possible to pipe a file upload using ruby? 是否可以使用ruby通过管道上传文件?
Instead of reading the whole file into memory, or writing a temp file to then write to S3, I was hoping to stream the request body directly. 我没有将整个文件读入内存,也没有写一个临时文件然后写入S3,而是希望直接流式传输请求主体。
Streaming data to S3 with ruby shows how to stream to S3, but I'm not sure how to turn the request body in a rack app into a readable stream. 使用ruby将数据流传输到S3显示了如何将数据流传输到S3,但是我不确定如何将机架应用程序中的请求主体转换为可读流。
class FastUploadApp
def self.call(env)
# what goes here?
end
end
You're exactly right that you need to turn the request body into a readable stream. 完全正确,您需要将请求正文转换为可读流。 Specifically, S3 expects a Ruby IO class (in that it wants a #read method and a #eof? method).
具体来说,S3需要一个Ruby IO类(因为它需要一个#read方法和一个#eof?方法)。 Rack request bodies don't have #eof?
机架请求主体没有#eof? defined, however, so you have to make a little wrapper class:
但是,必须定义一个包装类:
class RackS3Wrapper
def initialize(body)
@body = body
@eof = false
end
def read(*args)
ret = @body.read(*args)
if ret == nil or ret == ""
@eof = true
end
ret
end
def eof?
@eof
end
end
Then you can use this wrapper to stream the request to S3 directly: 然后,您可以使用此包装器将请求直接流式传输到S3:
s3.buckets['com.mydomain.mybucket'].objects['filename'].write(
:data => RackS3Wrapper.new(request.body),
:content_length => request.env['CONTENT_LENGTH'].to_i)
This hasn't been tested in production or anything, but it should work fine. 尚未经过生产或任何测试,但是应该可以正常工作。
Check out my project middl: https://github.com/diclophis/middl 查看我的项目Middl: https : //github.com/diclophis/middl
It has some caveats ... mostly you have to rely on the client sending an appropriate request size header (which is not required by the w3 spec) 它有一些警告...大多数情况下,您必须依靠客户端发送适当的请求大小标头(w3规范不需要)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.