无法从天蓝色存储中删除Blob

时间:2018-11-18 10:50:48

标签: c# azure azure-storage

我正在尝试从容器中删除Blob。 DeleteIfExits返回true,但是什么也没有发生。我使用Azure的门户检查了容器,但仍然可以看到斑点。

我的代码有什么问题?

   private static void DeleteAllFilesWithSameName(String filePath, String filename, CloudBlobContainer container)
    {

        String filenameWidthoutExtension = Path.GetFileNameWithoutExtension(filename);

        try
        {
            IEnumerable<IListBlobItem> blobs = container.ListBlobs(filenameWidthoutExtension, true);


            if (blobs.Count<IListBlobItem>() > 0)
            {
                List<string> blobNames = blobs.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();

                foreach (String blobName in blobNames)
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);

                    bool isDeleted = blockBlob.DeleteIfExists();

                }
            }
        }
        catch (Exception e)
        {
            Console.Write(e.Data);
        }


    }

1 个答案:

答案 0 :(得分:0)

我认为问题出在代码中的逻辑上

CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);

上面的行不是吗?

CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

请尝试通过将代码更改为:

                foreach (String blobName in blobNames)
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

                    bool isDeleted = blockBlob.DeleteIfExists();

                }