代码之家  ›  专栏  ›  技术社区  ›  Pரதீப்

如果目标中存在blob,如何覆盖blob

  •  0
  • Pரதீப்  · 技术社区  · 7 年前

    我正在使用 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 \" 已存在。“}系统。异常 {Microsoft.WindowsAzure.Storage.DataMovement.TransferSkippedException

    如果目标中存在文件,是否有覆盖该文件的选项?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Curiousdev    7 年前

    here

    我想你能做的是如下所示

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

    我不太清楚,但我在github上找到了一些测试用例 here