Don't Overwrite Azure Blob Storage

rahulchawla picture rahulchawla · Dec 8, 2017 · Viewed 9.9k times · Source

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)

Answer

Thomas picture Thomas · Dec 11, 2017

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.