Azure Blob 存储 - 上传有进度的文件

Azure Blob Storage - upload file with progress

我有以下代码 - 将文件上传到 Azure-Blob-Storage 很正常但是,当我上传文件而不是让 onProgress 执行多次时,我只执行(并且总是)执行一次file.size 值(因此它正在缓慢发送)文件到 azure,但进度仅在完成时执行一次。

    const requestOptions = this.mergeWithDefaultOptions(perRequestOptions);
    const client = this.getRequestClient(requestOptions);
    const containerClient = await client.getContainerClient(this.options.containerName);
    const blobClient = await containerClient.getBlockBlobClient(file.name);
    const uploadStatus = await blobClient.upload(file.buffer, file.size, {onProgress: progressCallBack});

我想知道这个结果对于这个库是否正常(对于从 azure 下载文件,同样的方法可以正常工作)。

根据我的测试,该方法是一种非并行上传方法,它只发送一个Put Blob request to Azure Storage server. For more details, please refer to here

所以如果你想多次执行onProgress,我建议你使用uploadStream的方法。它使用 Put Block operation and Put Block List operation to upload. For more details, please refer to here

例如

try {
    var creds = new StorageSharedKeyCredential(accountName, accountKey);
    var blobServiceClient = new BlobServiceClient(
      `https://${accountName}.blob.core.windows.net`,
      creds
    );
    var containerClient = blobServiceClient.getContainerClient("upload");
    var blob = containerClient.getBlockBlobClient(
      "spark-3.0.1-bin-hadoop3.2.tgz"
    );

    var maxConcurrency = 20; // max uploading concurrency
    var blockSize = 4 * 1024 * 1024; // the block size in the uploaded block blob
    var res = await blob.uploadStream(
      fs.createReadStream("d:/spark-3.0.1-bin-hadoop3.2.tgz", {
        highWaterMark: blockSize,
      }),
      blockSize,
      maxConcurrency,
      { onProgress: (ev) => console.log(ev) }
    );
    console.log(res._response.status);
  } catch (error) {
    console.log(error);
  }