0

I'm trying to simulate a move action (as I haven't seen any move method as such) by copying and deleting blobs between two directories within the same container.

For example, inside container A, moving .csv blobs from Folder_1 into Folder_2 and then deleting /year/month/day folder structure from Folder 1

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

What I currently have looks like the following piece of code:


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

The problem is that the tempBlobClient.delete() deletes the original a.csv but not the directory structure of Folder_1/year/month/day/. It leaves that path without a file but the directories remain undeleted.

Any ideas on how to deal with this problem?

Thanks a lot

basigow
  • 145
  • 1
  • 11
  • Is your storage account ADLS Gen2 or a regular storage account? From the description it looks like ADLS Gen2 but want to confirm. – Gaurav Mantri Sep 02 '21 at 12:41
  • Thanks for the reply Gaurav! It's ADLS Gen2 – basigow Sep 02 '21 at 12:43
  • 1
    For ADLS Gen2 Storage Accounts, please use `azure-storage-file-datalake` SDK instead of regular blob storage SDK. You can find more information about the SDK here: https://learn.microsoft.com/en-us/java/api/overview/azure/storage-file-datalake-readme?view=azure-java-stable. HTH. – Gaurav Mantri Sep 02 '21 at 12:46
  • This could be tried for java: https://stackoverflow.com/questions/70719419/delete-a-folder-in-azure-blob-storage-with-java/70719460#70719460 – Jatin Jan 15 '22 at 07:00

1 Answers1

0

try with this code

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();
}

For more details refer Thread.

ShrutiJoshi-MT
  • 1,622
  • 1
  • 4
  • 9