编程语言
首页 > 编程语言> > c# – 如何在容器之间复制blob?

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.

标签:c,azure,azure-storage-blobs
来源: https://codeday.me/bug/20190702/1354060.html