简体   繁体   中英

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

If I have my page blobs inside folder(CloudBlobDirectory) in source container I don't want to copy folder I want to copy just my page blobs to target container. How to do this?

You can use the TransferManager class included in the Azure Data Movement library.

You can copy each blob by executing:

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

Note that if you choose to use service side copy ('isServiceCopy' set to true), Azure (currently) doesn't provide SLA for that. Setting 'isServiceCopy' to false will download the source blob locally and upload it to the target blob.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM