Ruby Azure Blob存储:"RequestBodyTooLarge" [英] Ruby Azure Blob Storage: "RequestBodyTooLarge"

查看:60
本文介绍了Ruby Azure Blob存储:"RequestBodyTooLarge"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试使用演示代码将大文件(在这种情况下为388.7 MB)上传到Azure Blob存储,则失败如下:

If I try to upload a large file - 388.7 MB in this case - to azure blob storage using the demo code, it fails like so:

begin
  content = File.open("big_file.dat", "rb") { |file| file.read }
  blob = azure_blob_service.create_block_blob(container.name,"image-blob", content)
  puts blob.name
rescue StandardError => e
  $stderr.puts e.message
end

# RequestBodyTooLarge (413): The request body is too large and exceeds the maximum permissible limit.

我已经在blob存储文档中阅读到blob的最大大小为200 GB,因此Ruby API似乎没有正确地对其文件上传进行分块.我想念什么吗?

I've read in the blob storage documentation that blobs can be up to 200 GB in size, so it looks as though the Ruby API doesn't correctly chunk its file uploads. Am I missing something?

推荐答案

当前的Ruby Azure SDK确实具有执行分块上载的方法,但是在任何地方都没有使用示例,并且规范中的所有内容都是模拟的,而没有真的没有帮助.

The current Ruby Azure SDK does indeed have methods for doing chunked uploads, but there are no usage examples anywhere and everything in the specs is a mock, which doesn't really help.

让分块上载开始工作真是太奇妙了,这绝对是应该包含在库中的东西.我花了几个小时才能解决这个问题,希望这段代码对您有所帮助.

Getting chunked uploads to work is so fiddly that this is absolutely something that should be included in the library. It took me a number of hours to get this right and I hope this code snippet helps.

这是一个非常基本的用法示例:

Here is a very basic usage example:

class ::File
  def each_chunk(chunk_size=2**20)
    yield read(chunk_size) until eof?
  end
end

container  = 'your container name'
blob       = 'your blob name'
block_list = []
service    = Azure::BlobService.new
counter    = 1

open('path/to/file', 'rb') do |f|
  f.each_chunk {|chunk|
    block_id = counter.to_s.rjust(5, '0')
    block_list << [block_id, :uncommitted]

    # You will likely want to get the MD5 for retries
    options = {
      content_md5: Base64.strict_encode64(Digest::MD5.digest(chunk)),
      timeout:     300 # 5 minutes
    }

    md5 = service.create_blob_block(container, blob, block_id, chunk, options)
    counter += 1
  }
end

service.commit_blob_blocks(container, blob, block_list)

请给我几天,我应该对它进行更合理的封装,以提交给 https://github.com/dmichael/azure-contrib

Give me a couple of days and I should have something more reasonably encapsulated committed to https://github.com/dmichael/azure-contrib

这篇关于Ruby Azure Blob存储:"RequestBodyTooLarge"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆