Mattias
Mattias

Reputation: 3169

How to delete Azure blob from asp.net web api?

Uploading images from my web api to azure blob storage works fine, altough when trying to delete them i get the following error: "An exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code"

This is the code im using:

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

The code im trying to use comes from: https://azure.microsoft.com/sv-se/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs

Any help or input highly appreciated, thanks!

Upvotes: 2

Views: 1405

Answers (1)

You should make sure that the correct file name or container name. I use both methods.I'm sure it works in both.

Can you try code in below line ?

var result= blockBlob.DeleteIfExists();

Upvotes: 3

Related Questions