如果目标内存在,如何覆盖Blob

时间:2018-11-15 13:41:10

标签: c# azure copy azure-storage azure-storage-blobs

Am使用TransferManager将Blob从一个容器复制到另一个容器。

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();

但是,当文件位于目标位置时,它将引发错误说明

  

“跳过的文件   \“ https://sourceabcd.blob.core.windows.net/sourcecontainer/test1.txt \”   因为目标   \“ https://sourceabcd.blob.core.windows.net/destcontainer/test1.txt \”   已经存在。“} System.Exception   {Microsoft.WindowsAzure.Storage.DataMovement.TransferSkippedException

如果目的地中存在文件,是否可以选择覆盖文件?

1 个答案:

答案 0 :(得分:1)

可能是here

我认为您可以做的事情如下

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

我对此并不了解,但是我在github here上找到了一些测试用例

相关问题