Azure Block Blob:"The specified block list is invalid" 尝试创建块并提交它们时

Azure Block Blob: "The specified block list is invalid" when trying to create blocks and committing them

我正在尝试创建块并使用以下逻辑在相同代码中提交它们:

PutStream.js

const request = require('request')
const fs = require('fs')
const account = 'Enter your container name here'
const containerName = 'stream-test-container'
const blobName = 'myBlob.txt'
const strTime = new Date().toUTCString()
const _ = require('lodash')
let i = 0
const blockIdArray = []
const token = 'Enter Your Token Here'
const readStream = fs.createReadStream('smallTextFile.txt', { highWaterMark: 50 })

readStream.on('data', function (chunk) {
  i++
  const blobContent = chunk
  const contentLength = chunk.length
  const blockId = Buffer.from('block' + i).toString('base64')
  blockIdArray.push(blockId)
  const optionsPutBlock = {
    url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}?comp=block&blockid=${blockId}`,
    headers: {
      Authorization:
      `Bearer ${token}`,
      'x-ms-date': strTime,
      'x-ms-version': '2019-02-02',
      'Content-Length': contentLength
    },
    body: blobContent
  }
  request.put(optionsPutBlock, callbackPutBlock)
})

readStream.on('end', () => {
  const xmlBlockList = '<?xml version="1.0" encoding="utf-8"?><BlockList>' + (_.map(blockIdArray, (id) => { return `<Latest>${id}</Latest>` })).join('') + '</BlockList>'
  console.log(xmlBlockList)
  const xmlBlockListLength = new TextEncoder().encode(xmlBlockList).length
  const optionsPutBlockList = {
    url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}?comp=blocklist`,
    headers: {
      Authorization:
      `Bearer ${token}`,
      'x-ms-date': strTime,
      'x-ms-version': '2019-02-02',
      'Content-Length': xmlBlockListLength,
      'Content-Type': 'application/text-plain'
    },
    body: xmlBlockList
  }
  console.log(optionsPutBlockList.body)
  request.put(optionsPutBlockList, callbackPutBlockList)
})

function callbackPutBlock (error, response, body) {
  console.log(response.statusCode, response.statusMessage, Block created)
  if (error) {
    console.log(error)
  }
}

function callbackPutBlockList (error, response, body) {
  console.log(response.statusCode, response.statusMessage)
  if (error) {
    console.log(error)
  }
}

smallTextFile.txt的内容是:

This is a small file. Hello from Nodejs. How are you? 
This is line number 2. Dumm text Lorem
This is line number 3 of the dummy text
This is line number 4 four.

PutStream.js的输出:

<?xml version="1.0" encoding="utf-8"?><BlockList><Latest>YmxvY2sx</Latest><Latest>YmxvY2sy</Latest><Latest>YmxvY2sz</Latest><Latest>YmxvY2s0</Latest></BlockList>
201 Created Block created
400 The specified block list is invalid.
201 Created Block created
201 Created Block created
201 Created Block created

当我尝试 运行 时,它不会提交块。我在上面的代码 PutStream.js 运行 Get Block List 之后才知道这一点,因为所有块都在未提交列表中。

谁能说出为什么上面的逻辑在创建块后不容易提交?

参考文献:

  1. Put Block
  2. Get Block List
  3. Put Block List

问题 提交块:上面的代码甚至在 PutBlock 创建块之前就发送了 PutBlockList 请求,因此它失败了。

解决方案:通过将文件大小增加到 1Mb 并将高水印增加到 1024*256 并确保仅在所有块已上传。感谢 @mayank patel.

Note: keep length of the block ids same as issue may arise when the number of blocks become 10 or more ,as suggested by @gaurav mantri