无效的 Blob 或块内容错误,Node.js 个暂存块

Invalid Blob or Block content error, Node.js staging blocks

在这个问题上我一直在用头撞墙,它不起作用是没有任何意义的。所以我正在使用阶段和提交方法将文件上传到 Azure Blob。由于某种原因,在上传最后一个块时,它失败了。

单块上传代码

app.post("/upload/block", async (req, res) => {
    const { fileName, block } = req.body;
    const blobName = fileName;
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);

    // receive the blocks from client in JSON
    const { blockChecksum, blockNumber } = block; //
    const blockDataBytes = convertStringToBytes(block.blockData);


    const blockDataBytesChecksum = crypto.createHash('md5').update(blockDataBytes).digest('hex');
    if (blockChecksum !== blockDataBytesChecksum) {
        console.log("Block checksum mismatch");
        res.json({
            status: "FAILURE"
        });
        return;
    }
    const blockId = createBlockId(block);
    console.log({ blockIdLength: blockId.length, blockNumber, blockSize });

    let isError = false;

    await blockBlobClient.stageBlock(blockId, blockDataBytes, blockDataBytes.length)
        .catch((err) => {
            console.log(err);
            isError = true;
        })

    if (isError) {
        console.log("Error staging block");
        res.json({
            status: "FAILURE"
        });
        return;
    }
    console.log("Staged block");
    res.status(200).json({
        status: "SUCCESS"
    })
});

我正在按顺序从客户端上传块,当上传一个(收到成功响应)时,我会上传下一个。
服务器日志

{ blockIdLength: 64, blockNumber: 0, blockSize: 10485760 }
Staged block
{ blockIdLength: 64, blockNumber: 1, blockSize: 10485760 }
Staged block
{ blockIdLength: 64, blockNumber: 2, blockSize: 10485760 }
Staged block
{ blockIdLength: 64, blockNumber: 3, blockSize: 10485760 }
Staged block
{ blockIdLength: 64, blockNumber: 4, blockSize: 8306344 }
The specified blob or block content is invalid.
RequestId:8e343600-98d9-4c58-aadc-b5e707ccd749
Time:2022-04-27T15:24:32.598Z
Error staging block

最后一个块的大小是唯一的区别,它的校验和与客户端发送的内容和所有内容都匹配。我不知道这里出了什么问题。如您所见,我在日志中添加了块 ID 长度,以表明它也是常量。如果需要更多信息,我很乐意提供。

编辑 在此处创建块 ID:

const createBlockId = (block) => {
    const paddedBlockNumber = block.blockNumber.toString().padStart(6, "0");
    const blockString = `${paddedBlockNumber}_${block.blockSize}_${block.blockChecksum}`;
    return Buffer.from(blockString).toString('base64');
}

发现问题。我在创建块 ID 时犯了一个错误。块 ID 在 Azure 上的描述是:

Required. A valid Base64 string value that identifies the block. Prior to encoding, the string must be less than or equal to 64 bytes in size. For a given blob, the length of the value specified for the blockid parameter must be the same size for each block. Note that the Base64 string must be URL-encoded.

现在,在创建块 ID 时,我填充了块的数量而不是块的大小,这就是问题所在。

我正在检查 base64 编码块 ID 的长度,它仍然与服务器日志中看到的相同。

Finally: 转换前block Id的长度应该相同