0
votes

I have an Azure storage container with blobs (/images/filename). The filename(uri) is stored in the database at creation time and comes from the file upload save function:

        blob.UploadFromStream(filestream);
        string uri = blob.Uri.AbsoluteUri;
        return uri;

The file upload works fine and when passes to the client with SAS key download works fine too.

Coming to delete the images I have a helper function that was taken from a MS example here:MS Github example Here is the function:

    internal bool DeleteFile(string fileURI)
    {
        try
        {
            Uri uri = new Uri(fileURI);
            string filename = Path.GetFileName(uri.LocalPath);
            CloudBlockBlob fileblob = container.GetBlockBlobReference(filename);
            fileblob.Delete();
            bool res = fileblob.DeleteIfExists();
            return res; //Ok
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
            return false;
        }

    }

This is all in a helper class which starts as follows:

public class AzureHelpers
{
    private string connection;
    private CloudStorageAccount storageAccount;
    private CloudBlobClient blobClient;
    private CloudBlobContainer container;


    public AzureHelpers()
    {
        connection = CloudConfigurationManager.GetSetting("myproject_AzureStorageConnectionString");
        storageAccount = CloudStorageAccount.Parse(connection);
        blobClient = storageAccount.CreateCloudBlobClient();
        container = blobClient.GetContainerReference(Resources.DataStoreRoot);
        container.CreateIfNotExists();
    }
 ....

I deliberately added the delete before the deleteIfExists to cause the exception and prove what I suspected that it wasn't finding the file/blob.

As I step through the code however, the CloudBlockBlob is definitely there and has the correct URI etc.

I am wondering if this could be a permissions thing? Or am I missing something else?

2

2 Answers

4
votes

I think there is a directory in your container. Assume that you have a container named container_1, and your files are stored in directory like /images/a.jpg. Here you should remember that in this case, your blob name is images/a.jpg, not a.jpg.

In your code, Path.GetFileName only get the file name like a.jpg, so it does not match the real blob name images/a.jpg, which cause the error "does not exist".

So in your DeleteFile(string fileURI) method, try the code below, it works fine at my side:

Uri uri = new Uri(fileURI);
var temp = uri.LocalPath;
string filename = temp.Remove(0, temp.IndexOf('/', 1)+1);
CloudBlockBlob fileblob = container.GetBlockBlobReference(filename);
//fileblob.Delete();
bool res = fileblob.DeleteIfExists();

or use this code snippet:

Uri uri = new Uri(fileURI);

//use this line of code just to get the blob name correctly
CloudBlockBlob blob_temp = new CloudBlockBlob(uri);

var myblob = cloudBlobContainer.GetBlockBlobReference(blob_temp.Name);
bool res = myblob.DeleteIfExists();
0
votes

Seems to be a permission issue, Can you goto portal and then Edit container meta data on azure bolb storage. change access private to public.