How to overwrite blob if exists in destination

Viewed 3218

Am using TransferManager to copy blobs from one container to another.

CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(@"source storage account connection string");
CloudStorageAccount destStorageAccount = CloudStorageAccount.Parse(@"destination storage account connection string");

CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobClient destBlobClient = destStorageAccount.CreateCloudBlobClient();
var sourceContainer = sourceBlobClient.GetContainerReference("sourceContainer");
var destContainer = destBlobClient.GetContainerReference("destContainer");

CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("copy.txt");
CloudBlockBlob targetBlob = destContainer.GetBlockBlobReference("copy.txt");

TransferManager.CopyAsync(sourceBlob, targetBlob, true).Wait();

But when the file exists in destination it throws error stating

"Skiped file \"https://sourceabcd.blob.core.windows.net/sourcecontainer/test1.txt\" because target \"https://sourceabcd.blob.core.windows.net/destcontainer/test1.txt\" already exists."} System.Exception {Microsoft.WindowsAzure.Storage.DataMovement.TransferSkippedException

Is there a option to overwrite the file if exists in destination ?

1 Answers

Probably you're getting exception from here

i think what you can do is something like below

TransferContext transferContext = new SingleTransferContext();
transferContext.ShouldOverwriteCallbackAsync = TransferContext.ForceOverwrite;
TransferManager.CopyAsync(sourceBlob, targetBlob, true,null,transferContext).Wait();

I don't have much idea about it but i found some test cases on github here

Related