Remove blob inside virtual directory
I am trying to delete a blob inside a virtual directory, with no luck. The 404 error is always returned.
The url file looks something like this: https://XXX.blob.core.windows.net/YY/pdf/pdf/file.pdf where XXX is my account and YY is the container.
I tried to remove trhough it full name (pdf / pdf / file.pdf, no luck.
blockBlob = container.GetBlockBlobReference("pdf/pdf/file.pdf"); //or
blockBlob = container.GetBlockBlobReference("file.pdf");
Also tried navigating virtual folders (GetDirectoryReference) and deleting files when I get to it with no luck either.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("YY");
CloudBlockBlob blockBlob;
string sBlob = fullUrl.Substring(fullUrl.IndexOf("YY/") + ("YY/").Length);
string[] sDirs = sBlob.Split(new Char[] { '/' });
if (sDirs.Length > 0)
{
CloudBlobDirectory dir = container.GetDirectoryReference(sDirs[0]);
if (sDirs.Length > 2)
{
for (int i = 1; i < sDirs.Length - 2; i++)
{
dir = dir.GetDirectoryReference(sDirs[i]);
}
}
blockBlob = dir.GetBlockBlobReference(sDirs[sDirs.Length - 1]);
}
else
{
blockBlob = container.GetBlockBlobReference(sBlob);
}
blockBlob.Delete(); //when debugging, I can see blockBlob properties here
Note. I can successfully delete files in the root directory ( https://XXX.blob.core.windows.net/YY/file.pdf ).
Mistake:
blockBlob.Delete()' threw an exception of type 'Microsoft.WindowsAzure.Storage.StorageException'
base: {"The remote server returned an error: (404) Not found."}
RequestInformation: {Microsoft.WindowsAzure.Storage.RequestResult}
UPDATE:
I was able to remove this code:
foreach (var blobItem in container.ListBlobs(useFlatBlobListing: true)) {
if (blobItem.Uri.ToString() == caminho) {
blockBlob.Delete();
return "";
}
}
... but that seems like a crazy solution ...: /
+3
source to share
No one has answered this question yet
Check out similar questions: