Posted on

Got interesting exception today while trying to upload multiple file blobs to Azure container.

 

private void UploadFileToBlob(string url, Asset asset,string filepath,string filename)
       {
           var container = new CloudBlobContainer(url);

           //Get a reference to a blob, which may or may not exist.
           CloudBlob blob = container.GetBlobReference(filename);
           blob.DeleteIfExists();
           //Upload content to the blob, which will create the blob if it does not already exist.
           blob.UploadFile(filepath);
       }

When I am executing UploadFileToBlob for one file it is working. On different multiple files it is throwing exception “StorageClient.StorageClientException: The specified blob already exists”.

Did some research and found out that this problem associated with multithreading in local dev storage:



This is a known issue with development storage. This happens when there are multiple threads launched to upload the blocks (which constitute the blob). Basically what is happening is that development storage makes use of SQL Server as the data store. Now first thing it does is makes an entry into the table which stores blob information. If there are multiple threads working then all of these threads will try to perform the same operation. After the first thread succeeds, the subsequent threads will result in this exception being raised.