如果我尝试使用演示代码将一个大文件(本例中为388.7 MB)上载到azure blob存储,它将失败,如下所示:

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的大小可以高达200gb,因此看起来Ruby API没有正确地将文件上传分块我遗漏了什么吗?

最佳答案

当前的Ruby Azure SDK确实有方法进行分块上传,但是在任何地方都没有使用示例,而且规范中的所有内容都是模拟的,这并没有真正的帮助。
让分块上传工作非常繁琐,这绝对是应该包含在库中的东西我花了好几个小时才搞定这一点,我希望这段代码能有所帮助。
下面是一个非常基本的用法示例:

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

关于ruby - Ruby Azure Blob存储:“RequestBodyTooLarge”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23567724/

10-12 00:25
查看更多