Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a blob from Windows azure in c#

I have code which inserts a blob into storage, and allows the user to view a list of the blobs, and an individual blob. However, I now can't get the blob to delete, the error that appears is

"An exception of type 'System.ServiceModel.FaultException`1' occurred in System.ServiceModel.ni.dll but was not handled in user code. Additional information: The remote server returned an error: (404) Not Found."

The code in the WCF service is

public void DeleteBlob(string guid, string uri)
{
    //create the storage account with shared access key
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(accountDetails);

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(guid);

    CloudBlockBlob blob = container.GetBlockBlobReference(uri);
    blob.DeleteIfExists();
}

and then I access this in the mobile client application through SOAP services like:

private void mnuDelete_Click(object sender, EventArgs e)
{
    MessageBoxResult message = MessageBox.Show("Are you sure you want to delete this image?", "Delete", MessageBoxButton.OKCancel);
    if (message == MessageBoxResult.OK)
    {
        Service1Client svc = new Service1Client();
        svc.DeleteBlobCompleted += new EventHandler<AsyncCompletedEventArgs>(svc_DeleteBlobCompleted);
        svc.DeleteBlobAsync(container, uri);
    }
}
void svc_DeleteBlobCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error == null) {
        NavigationService.Navigate(new Uri("/Pages/albums.xaml", UriKind.Relative));
    }
    else {
        MessageBox.Show("Unable to delete this photo at this time", "Error", MessageBoxButton.OK);
    }
}

I also use SAS token to save the blob in the first place - I don't know whether this makes a difference?

like image 359
Bryony Lloyd Avatar asked Feb 10 '23 14:02

Bryony Lloyd


1 Answers

In Azure Storage Client Library 4.0, we changed Get*Reference methods to accept relative addresses only. So, if you are using the latest library and the parameter "uri" is an absolute address, you should change it to either to the blob name or you should use the CloudBlockBlob constructor that takes an Uri and a StorageCredentials object.

Please see all such breaking changes in our GitHub repository.

like image 135
Serdar Ozler Avatar answered Feb 13 '23 02:02

Serdar Ozler