【问题标题】:Azure blob deletion using @azure/storage-blob and nodejs使用 @azure/storage-blob 和 nodejs 删除 Azure blob
【发布时间】:2022-01-28 18:01:03
【问题描述】:

我希望删除 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();

感谢您的帮助

【问题讨论】:

    标签: azure-functions azure-blob-storage


    【解决方案1】:

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

    blockBlobClient.deleteIfExists(DeleteSnapshotsOptionType.INCLUDE);
    

    delete(BlobDeleteOptions)deleteIfExists(BlobDeleteOptions) 中包含以下有助于过滤删除操作的选项类型

    选项属性是 abortSignalconditionscustomerProvidedKeydeleteSnapshotstracingOptions

    DeleteSnapshots 我们有 2 个选项

    1. 包括:删除基础 blob 及其所有快照。
    2. only:仅删除 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();
    

    【讨论】: