【问题标题】:How to Delete a blob from Azure blob v12 SDK for Node.js如何从适用于 Node.js 的 Azure blob v12 SDK 中删除 blob
【发布时间】:2020-06-28 04:57:57
【问题描述】:

如何通过 Node.js 删除 Azure Blob,并且我正在使用 Azure library v12 SDK for Node.js (https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs)

我找不到删除 blob 方法,我想按名称删除 blob。

【问题讨论】:

标签: node.js azure azure-blob-storage


【解决方案1】:

正如@Georage 在评论中所说,您可以使用delete 方法删除一个blob。

这是我的演示:

const { BlobServiceClient,ContainerClient, StorageSharedKeyCredential } = require("@azure/storage-blob");

// Load the .env file if it exists
require("dotenv").config();

async function streamToString(readableStream) {
    return new Promise((resolve, reject) => {
      const chunks = [];
      readableStream.on("data", (data) => {
        chunks.push(data.toString());
      });
      readableStream.on("end", () => {
        resolve(chunks.join(""));
      });
      readableStream.on("error", reject);
    });
  }

async function main() {
    const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
    const blobServiceClient = await BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
    const containerClient = await blobServiceClient.getContainerClient("test");
    const blockBlobClient = containerClient.getBlockBlobClient("test.txt")
    const downloadBlockBlobResponse = await blockBlobClient.download(0);
    console.log(await streamToString(downloadBlockBlobResponse.readableStreamBody));
    const blobDeleteResponse = blockBlobClient.delete();
    console.log((await blobDeleteResponse).clientRequestId);
}

main().catch((err) => {
    console.error("Error running sample:", err.message);
  });

运行此示例后,test.txt 文件已从 test 容器中删除。

【讨论】:

    【解决方案2】:

    虽然杰克的答案有效,但它比需要的复杂。与其创建 blockBlobClient 然后删除它,更简单的方法是使用:

    containerClient.deleteBlob('blob-name')

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2019-11-28
      • 2021-10-05
      • 2021-10-31
      • 2021-02-15
      • 2021-01-04
      • 2020-08-22
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多