删除"子路径"来自Azure存储

时间:2015-04-19 12:00:48

标签: azure azure-storage azure-storage-blobs

我知道Azure没有实际的子路径,但如果我有例如container/projectID/iterationNumber/filename.jpg并删除项目,我该如何从ProjectID删除?是否可以通过编码?

我不想使用azure应用程序,因为我正在创建一个Web应用程序。

先谢谢

修改

这是Microsoft提供的针对特定项目的代码:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.txt");

// Delete the blob.
blockBlob.Delete(); 

SystemDesignModel

    public static SystemDesign returnImageURL(IListBlobItem item)
    {

        if (item is CloudBlockBlob)
        {
            var blob = (CloudBlockBlob)item;
            return new SystemDesign
            {
                URL = blob.Uri.ToString(),
            };

        }
        return null;
    }

}

1 个答案:

答案 0 :(得分:3)

如您所知,blob存储没有子文件夹的概念。它只有2级层次结构 - container& blobs。所以从本质上讲,子文件夹只是附加到blob名称的前缀。在您的示例中,您上传的实际文件为filename.jpg,但其从blob存储角度来看的名称为projectID/iterationNumber/filename.jpg

由于没有子文件夹的概念,您无法像我们在本地计算机上那样删除它。但是有一种方法。 Blob存储提供了search for blobs starting with a certain blob prefix的方法。所以你要做的就是首先列出所有以特定前缀(在你的情况下为projectID)开头的blob,然后在列表操作的结果中一次删除一个blob。

请看下面的示例代码:

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
        var container = storageAccount.CreateCloudBlobClient().GetContainerReference("container");
        BlobContinuationToken token = null;
        do
        {
            var listingResult = container.ListBlobsSegmented("blob-prefix (projectID in your case)", true, BlobListingDetails.None, 5000, token, null, null);
            token = listingResult.ContinuationToken;
            var blobs = listingResult.Results;
            foreach (var blob in blobs)
            {
                (blob as ICloudBlob).DeleteIfExists();
                Console.WriteLine(blob.Uri.AbsoluteUri + " deleted.");
            }
        }
        while (token != null);