azure - 从 Azure 存储中删除 "subpath"

标签 azure azure-storage azure-blob-storage

我知道 Azure 没有实际的子路径,但如果我有 container/projectID/iterationNumber/filename.jpg 并且我删除了一个项目,如何从 ProjectID 中删除?通过编码可以实现吗?

我不想在创建 Web 应用程序时使用 Azure 应用程序。

提前致谢

编辑:

这是 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(); 

系统设计模型

    public static SystemDesign returnImageURL(IListBlobItem item)
    {

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

        }
        return null;
    }

}

最佳答案

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

由于没有子文件夹的概念,因此您无法像我们在本地计算机上那样删除它。不过有一个办法。 Blob 存储提供了一种搜索以特定 Blob 前缀开头的 Blob的方法。因此,您要做的就是首先列出以特定前缀(在您的情况下为 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);

关于azure - 从 Azure 存储中删除 "subpath",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729570/

相关文章:

Azure 存储服务日志

azure-blob-storage - 将 csv 数据直接写入 azure blob 存储

azure - 将 Blob 存储 key 更新到应用程序

azure - Pulumi GitHub Action pulumi/actions@v2 : error: It looks like the Pulumi SDK has not been installed. 你运行过npm install 或yarn install 吗?

azure - 表的 Azure 存储服务 REST API 授权

azure - 无法在 csharp 中设置 Azure Data Lake Gen2 中 blob 的 ContentType

azure - 从代码访问 Microsoft.Storage/storageAccounts/blobServices 指标

c# - Azure Function 返回多个 JSON 对象

azure - 使用 sql server 创建 Microsoft Azure Web App 失败

Azure ML 工作区无法为 VNet 后面的工作区链接存储帐户上传数据