5

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)

rahulchawla
  • 170
  • 1
  • 3
  • 20
  • If I understand correctly, you would want to add new content to an existing blob instead of overwriting it. Am I correct? Or do you want the operation to fail if the blob already exists? – Gaurav Mantri Dec 08 '17 at 14:56
  • Correct @GauravMantri - Add new content to Existing blob – rahulchawla Dec 08 '17 at 14:59
  • From your code it seems that you're uploading images. Is that going to be the target data? Because if that's the case then your blob would be corrupted and become essentially useless. Or are you trying to save some logs where you would want to append the latest data to an existing blob? – Gaurav Mantri Dec 08 '17 at 15:07
  • I basically want to use the blob storage as a storage for the images, why would it become corrupt - i will be transferring the latest data but it will be image files @GauravMantri – rahulchawla Dec 08 '17 at 15:14
  • The reason they would corrupt is because you want to append new data to an existing blob. So let's say you have an image called image1.png of 10KB in blob storage. Now you want to append some more data to that file only. It will corrupt in this scenario. Or am I not understanding your use case? If that's the case, please edit your question and include more details about your requirements (please do not provide these details in comments). – Gaurav Mantri Dec 08 '17 at 15:16
  • No - I want to keep adding images, for example I'm processing images and adding individual images to the container NOT APPENDING TO THE FILE and not overwriting the blob @GauravMantri – rahulchawla Dec 08 '17 at 15:22
  • Aah...so you want to append the images to a container (in other words keep on adding images to the container). If the file exists, then you would not want to overwrite the existing file). May be save them with a different name? Is this correct? – Gaurav Mantri Dec 08 '17 at 15:24
  • 2
    Ok let's not turn this into a discussion forum. @rahulchawla - please *edit your question* to clarify further. Lots of details are being buried here. And again, StackOverflow is not set up to be a discussion forum. This is turning into a comment stream. But I'm pretty sure Gaurav has properly identified your issue, and your misunderstanding of blob storage, and should simply post an answer at this time. – David Makogon Dec 08 '17 at 15:25
  • @GauravMantri Correct! – rahulchawla Dec 08 '17 at 15:44

2 Answers2

3

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.
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • 2
    Note that this has changed in v2.34.1 of the Azure CLI. Per [the changelog](https://learn.microsoft.com/en-us/cli/azure/release-notes-azure-cli#march-03-2022), 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.** – MandM Mar 11 '22 at 20:54
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.

Thomas
  • 24,234
  • 6
  • 81
  • 125