简体   繁体   English

如何将通过机架上传的文件直接传送到S3?

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

相关问题 如何在使用jquery文件上传将原始文件直接上传到s3后使用carrierwave创建缩略图 - How to create thumbnails with carrierwave after uploading the original file directly to s3 with jquery file upload 如何使文件上传保持您原来的“内容类型”到 Amazon S3? - How can I to make upload of file maintain your original `Content Type` to Amazon S3? 在遵循Heroku关于如何直接上传到AWS S3的教程之后,无法将ActionDispatch :: Http :: UploadedFile转换为字符串 - can't cast ActionDispatch::Http::UploadedFile to string after following Heroku's tutorial on how to upload directly to aws s3 如何在Rack应用程序运行时调试它? - How can I debug my Rack app while it's running? 我可以使用aws-sdk-ruby在AWS S3上使用事务文件删除/上传吗? - Can I use transactional file remove/upload on AWS S3 with aws-sdk-ruby? 如何使用雾来编辑s3上的文件? - How can I use fog to edit a file on s3? 如何使用连接到模型的Paperclip has_attached_file直接上传到S3? - How do I upload direct to S3 with Paperclip has_attached_file connected to a model? 如何通过载波在S3中上传图像并将文件名保存在数据库中? - How can I upload image in S3 by carrierwave and save filename in database? 如何将大于5GB的文件上传到Amazon S3? - How can I upload files larger than 5GB to Amazon S3? 您如何上传一个zip文件并解压缩到s3? - How do you upload a zip file and unzip to s3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM