如何使用 Java SDK v12 删除 Blob 文件夹

How to delete Blob folders using Java SDK v12

我正在尝试通过在同一容器内的两个目录之间复制和删除 blob 来模拟移动操作(因为我还没有看到任何移动方法)。

例如,在 container A 中,将 .csv blob 从 Folder_1 移动到 Folder_2,然后从 Folder 1 中删除 /year/month/day 文件夹结构

container A
|_ Folder_1
|_ _ _/year/month/day/a.csv
|
|_ Folder_2

我目前拥有的代码类似于以下代码:


String blobUrl = "Folder_1 a.csv blob url"
String blobName = "a.csv"
BlobContainerClient outputContainerClient = outputBlobServiceClient.getBlobContainerClient("Container A");
// Folder_1 client
BlobClient tempBlobClient=outputContainerClient.getBlobClient("Folder_1/year/month/day/" + blobName);
// Folder_2 client
BlobClient destBlobClient=outputContainerClient.getBlobClient("Folder_2/year/month/day/" + blobName);
// Copy from Folder_1 to Folder_2                
destBlobClient.beginCopy(blobUrl,null);
// Delete Folder_1      
tempBlobClient.delete();  

问题是 tempBlobClient.delete() 删除了原来的 a.csv 而不是 Folder_1/year/month/day/ 的目录结构。它使该路径没有文件,但目录仍未删除。

关于如何处理这个问题有什么想法吗?

非常感谢

尝试使用此代码

var connectionString = "blob-connection-string";
var containerName = "container-name";
var folderPath = "folder1/subfolder/sub-subfolder";

var blobServiceClient = new BlobServiceClient(connectionString);
var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobItems = blobContainerClient.GetBlobsAsync(prefix: folderPath);
await foreach (BlobItem blobItem in blobItems)
{
     BlobClient blobClient = blobContainerClient.GetBlobClient(blobItem.Name);
     await blobClient.DeleteIfExistsAsync();
}

有关详细信息,请参阅