c# – 如何在容器之间复制blob?

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复制到目标容器.这该怎么做?

最佳答案 您可以使用
Azure Data Movement库中包含的
TransferManager class.

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

await TransferManager.CopyAsync(sourceBlob, targetBlob, isServiceCopy: false);

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

点赞