Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload to Azure Blob storage without overwriting?

Calling UploadFromStream overwrites files by default - how can I make sure I only upload a blob if it isn't already in the container?

CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); blockBlob.UploadFromStream(stream) 
like image 235
Rob Church Avatar asked Feb 18 '13 14:02

Rob Church


People also ask

What is append blob storage?

An append blob is composed of blocks and is optimized for append operations. When you modify an append blob, blocks are added to the end of the blob only, via the Append Block operation. Updating or deleting of existing blocks is not supported. Unlike a block blob, an append blob does not expose its block IDs.

How do I copy files from Azure Blob storage to another blob storage?

Copy all containers, directories, and blobs to another storage account by using the azcopy copy command. This example encloses path arguments with single quotes (''). Use single quotes in all command shells except for the Windows Command Shell (cmd.exe).


1 Answers

Add an access condition to the code so that it checks against the ETag property of the blob - wildcards are allowed, so we want to only allow the upload if no blobs with this name have any etag (which is a roundabout way of saying, does this blob name exist).

You get a StorageException as detailed below.

    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);     try {         blockBlob.UploadFromStream(stream, accessCondition: AccessCondition.GenerateIfNoneMatchCondition("*"));     } catch (StorageException ex) {         if (ex.RequestInformation.HttpStatusCode == (int)System.Net.HttpStatusCode.Conflict) {             // Handle duplicate blob condition         }         throw;     }      
like image 74
Rob Church Avatar answered Sep 23 '22 22:09

Rob Church