使用@azure/storage-blob 和 nodejs 删除 Azure blob

Azure blob deletion using @azure/storage-blob and nodejs

我想删除 blob 及其快照。要传递以包含快照删除的参数或条件是什么。下面是我的代码:

const blobServiceClient = require("./getCred");

async function deleteRecord() {
  try {
    const containerClient = blobServiceClient.getContainerClient("containerName");
    const blockBlobClient = containerClient.getBlockBlobClient("dummy.json");
    const deleteBlobResponse = await blockBlobClient.deleteIfExists();
    console.log(`Deleted  successfully ${deleteBlobResponse.requestId}`);
    return `Deleted  successfully ${deleteBlobResponse.requestId}`;
  } catch (err) {
    console.log(err);
    return err;
  }
}

deleteRecord();

感谢帮助

您需要在 deleteIfExists() 您的代码中包含 DeleteSnapshotsOptionType。请按照以下解决方法

blockBlobClient.deleteIfExists(DeleteSnapshotsOptionType.INCLUDE);

delete(BlobDeleteOptions) & deleteIfExists(BlobDeleteOptions) 中包含以下选项类型,可以帮助过滤删除操作

选项属性是 abortSignal , conditions , customerProvidedKey , deleteSnapshots, and tracingOptions

DeleteSnapshots中我们有2个选项

  1. include:删除基础 blob 及其所有快照。
  2. :仅删除 blob 的快照,而不删除 blob 本身。

更改后 blockBlobClient.deleteIfExists(DeleteSnapshotsOptionType.INCLUDE); 它能够连同快照一起删除 blob。

更改源代码:

const blobServiceClient = require("./getCred");
async function deleteRecord() {
    try {
        const containerClient = blobServiceClient.getContainerClient("containerName");
        const blockBlobClient = containerClient.getBlockBlobClient("dummy.json");
        const deleteBlobResponse = await blockBlobClient.deleteIfExists( DeleteSnapshotsOptionType.INCLUDE);
        console.log(`Deleted  successfully ${deleteBlobResponse.requestId}`);
        return `Deleted  successfully ${deleteBlobResponse.requestId}`;
    
    } catch (err) {
        console.log(err);
        return err;
    }
}

deleteRecord();