如何在容器之间复制 blob?

How to copy blobs between containers?

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");
}

如果我的页面 blob 位于源容器中的文件夹 (CloudBlobDirectory) 中 我不想复制文件夹我只想将我的页面 blob 复制到目标容器。如何做到这一点?

您可以使用 TransferManager class included in the Azure Data Movement 库。

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

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

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