如何使用 Azure 客户端库 v12 将整个容器从一个 Azure Blob 服务复制到另一个

问题描述

我正在尝试使用 Azure 编程工具,特别是尝试将容器从一个 Azure Blob 存储帐户复制/克隆/移动到另一个帐户。尽管在其文档中使用 v12.x 客户端库(至少到目前为止),但我没有看到这样做的好方法

例如:

BlobStorage1
|-- SomeContrainer
    |-- blob1
    |-- blob2
    |-- blob3

BlobStorage2
|-- SomeOtherContrainer
    |-- otherBlob

我想以编程方式将 SomeContainer 及其所有 blob 移动到 BlobStorage2。

到目前为止我尝试过的:

文档阅读/咨询:

这似乎没有显示如何从 account1 pushed 到 account2。您可以从 account2 下拉到 account 1,但这与我所追求的相反。

代码尝试:

var localContainer = blobServiceClient.GetBlobContainerClient(localContainerName);
var blobs = localContainer.GetBlobs();

var remoteClient = new BlobServiceClient(remoteConnectionString);
var remoteContainer = remoteClient.GetBlobContainerClient(localContainerName);

foreach(var blob in blobs)
{
    Console.WriteLine($"copying blob: {blob.Name}");
    var sourceBlob = localContainer.GetBlobClient(blob.Name);
    var remoteBlobClient = remoteContainer.GetBlobClient(blob.Name);
    await remoteBlobClient.StartcopyFromUriAsync(sourceBlob.Uri);
}

这里的问题是我可以从远程复制到本地(通过连接字符串),或者在同一个帐户内复制,因为 URI 非常相似,但不能从我所在的帐户复制到单独的存储帐户。使用客户端库 (v12.x) 将 blob(或容器整体销售,如果可能)从一个帐户复制到另一个帐户的推荐方法是什么?

解决方法

请尝试以下代码:

    static async Task CopyContainersAcrossAccounts()
    {
        var sourceAccountName = "source-account";
        var sourceAccountKey = "source-account-key";
        var targetAccountName = "target-account";
        var targetAccountKey = "target-account-key";
        Azure.Storage.StorageSharedKeyCredential sourceCredential = new Azure.Storage.StorageSharedKeyCredential(sourceAccountName,sourceAccountKey);
        Azure.Storage.StorageSharedKeyCredential targetCredential = new Azure.Storage.StorageSharedKeyCredential(targetAccountName,targetAccountKey);
        var sourceConnectionString = $"DefaultEndpointsProtocol=https;AccountName={sourceAccountName};AccountKey={sourceAccountKey};EndpointSuffix=core.windows.net;";
        var sourceContainer = "source-container-name";
        var targetConnectionString = $"DefaultEndpointsProtocol=https;AccountName={targetAccountName};AccountKey={targetAccountKey};EndpointSuffix=core.windows.net;";
        var targetContainer = "target-container-name";
        var sourceBlobContainerClient = new Azure.Storage.Blobs.BlobContainerClient(sourceConnectionString,sourceContainer);
        
        var targetBlobContainerClient = new Azure.Storage.Blobs.BlobContainerClient(targetConnectionString,targetContainer);
        var sourceBlobs = sourceBlobContainerClient.GetBlobs();
        foreach (var blob in sourceBlobs)
        {
            //Get shared access signature with "Read" permission in case source container is a private container.
            Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new Azure.Storage.Sas.BlobSasBuilder()
            {
                BlobContainerName = sourceContainer,BlobName = blob.Name,ExpiresOn = DateTime.UtcNow.AddHours(1)
            };
            blobSasBuilder.SetPermissions(Azure.Storage.Sas.BlobSasPermissions.Read);
            var sasToken = blobSasBuilder.ToSasQueryParameters(sourceCredential).ToString();
            var sourceBlobClient = sourceBlobContainerClient.GetBlobClient(blob.Name);
            var targetBlobClient = targetBlobContainerClient.GetBlobClient(blob.Name);
            var sourceBlobUri = new Uri($"{sourceBlobClient.Uri.AbsoluteUri}?{sasToken}");
            await targetBlobClient.StartCopyFromUriAsync(sourceBlobUri);
        }
    }