Don't Overwrite Azure Blob Storage

12,358

Solution 1

By design, Azure Blob Storage will overwrite the contents of a blob if you're trying to upload new content with same blob name.

If you want to don't want to override the contents, there are certain things you could do but then you will have to write code for it.

  1. Check if blob exists before uploading and fail the upload operation and notify the user to either pick another name for the blob or assign a new name in your code. You could check for blob's existence by calling blob.ExistsAsync or perform the operation using an access condition like you're doing currently. In the 1st case, you will get a true/false value back and based on that you decide what you want to do. In the 2nd case, the uploading operation will fail with PreConditionNotMet (412 Status Code) so you will need to catch the exception and decide what you want to do.
  2. If your intention is to keep a history of all the changes, then you should look at blob snapshot feature available in Azure Blob Storage. When you take a blob's snapshot, it creates a read-only copy of the blob. However please do keep in mind that in this case, there will just one blob and many snapshots. Each snapshot is uniquely identified by the date/time when it was snapshotted. You can fetch a blob and its snapshots to see the history of the changes done to that blob.

Solution 2

There is now a CloudAppendBlob class that allows you to add content to an existing blob :

var account = CloudStorageAccount.Parse("storage account connectionstring");
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("container name");
var blob = container.GetAppendBlobReference("blob name");

In your case you want to append from a file:

await blob.AppendFromFileAsync("file path");

But you can append from text, byte array, stream. Check the documentation.

Share:
12,358
rahulchawla
Author by

rahulchawla

Updated on June 04, 2022

Comments

  • rahulchawla
    rahulchawla almost 2 years

    I have a method which adds files to a Azure Blob Storage the problem is i'm trying to specify a condition in which it DOES NOT overwrite the blob but simply adds to it. I am trying to use the parameter access condition however VS is saying this method cannot take two parameters- async void archiveNewImportedImages(List imageFiles) {

            // Saving the images
            // Retrieve reference to a blob named "myblob".
    
    
            // Create or overwrite the "myblob" blob with contents from a local file.
    
            using (var fileStream = System.IO.File.OpenRead(@"C:\Users\rahulchawla\Desktop\FilezilleIMGS\FilezilleIMGS\MTO_Image\CR01-1-20170623-1327.jpg"))
            {
    
                await blockBlob.UploadFromStreamAsync(fileStream, accessCondition: AccessCondition.GenerateIfNoneMatchCondition("*"));
            }
    
            // save url of the image into a variable and later to the database
            ///  fileURL = blockBlob.Uri.ToString();
    
    
    
        }
    

    Any suggestions?

    end goal: dont overwrite container - keep adding distinct files ex. img1.jpg, img2.jpg to blob

    Additional Details: Want to append the images to a container (in other words keep on adding images to the container). If the file exists, then would not want to overwrite the existing file)

  • MandM
    MandM about 2 years
    Note that this has changed in v2.34.1 of the Azure CLI. Per the changelog, a breaking change has been introduced that introduces an --overwrite flag and, if omitted, the blob will not be overwritten and the following error will be thrown: The specified blob already exists.