如何在容器之间复制blob?

时间:2016-06-07 16:32:38

标签: c# azure azure-storage-blobs

public void CopyBlobsBetweenContainers(string sourceContainerName, string targetContainerName, IEnumerable<string> blobsToCopy)
{
    foreach (var sourceBlobName in blobsToCopy)
    {
        _cancellationToken.ThrowIfCancellationRequested();

        var sourceBlob = sourceContainer.GetPageBlobReference(sourceBlobName);
        if (!sourceBlob.Exists())
        {
            throw new InvalidOperationException(SafeFormatter.Format(CloudLocalization.Culture, CloudLocalization.CannotCopyBlobDoesNotExist, sourceBlobName));
        }

        var targetBlob = targetContainer.GetPageBlobReference(sourceBlobName);
        if (targetBlob.Exists())
        {
            throw new InvalidOperationException(SafeFormatter.Format(CloudLocalization.Culture, CloudLocalization.CannotCopyTargetAlreadyExists, sourceBlobName));
        }

        _logger.Debug("Copying blob '{0}' from container '{1}' to '{2}'", sourceBlobName, sourceContainerName, targetContainerName);

        targetBlob.Create(sourceBlob.Properties.Length);
        targetBlob.StartCopy(sourceBlob);
    }

    _logger.Debug("Copy blobs finished");
}

如果我在源容器中的文件夹(CloudBlobDirectory)中有我的页面blob 我不想复制文件夹我只想将我的页面blob复制到目标容器。怎么做?

1 个答案:

答案 0 :(得分:2)

您可以使用TransferManager class库中包含的Azure Data Movement

您可以通过执行以下命令复制每个blob:

@Test(dependsOnMethods = {"testMethod1"},alwaysRun=true)
public void testMethod2(){
}

请注意,如果您选择使用服务端副本(&#39; isServiceCopy&#39;设置为true),则Azure(当前)不会为此提供SLA。设置&#39; isServiceCopy&#39; to false将在本地下载源blob并将其上载到目标blob。