【问题标题】:C# :Move blobs from one container to another using Azure.Storage.Blobs client libraryC#:使用 Azure.Storage.Blobs 客户端库将 blob 从一个容器移动到另一个容器
【发布时间】:2021-10-10 13:31:22
【问题描述】:

我正在使用最新的 Azure.Storage.Blobs 客户端库。我在 CloudblockBlob 客户端中看到了很多使用 StartCopyAsync() 方法进行复制和删除的示例,但是对于较新的版本,我找不到任何东西。

我需要将文件从一个容器移动到同一个存储帐户中的另一个容器。

这是旧版本

      CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
    CloudBlobContainer sourceContainer = blobClient.GetContainerReference(SourceContainer);
    CloudBlobContainer targetContainer = blobClient.GetContainerReference(TargetContainer);
        
    CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(fileToMove);
    CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(newFileName);
                    await targetBlob.StartCopyAsync(sourceBlob);

【问题讨论】:

    标签: azure-blob-storage


    【解决方案1】:

    请尝试以下代码。它使用Azure.Storage.Blobs (12.9.1)

    using System;
    using System.Threading.Tasks;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    
    namespace SO68668289
    {
        class Program
        {
            private const string connectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key==";
            private const string sourceContainer = "source";
            private const string targetContainer = "target";
            private const string blobName = "blob-name.txt";
    
            static async Task Main(string[] args)
            {
                BlobServiceClient serviceClient = new BlobServiceClient(connectionString);
                BlobContainerClient sourceContainerClient = serviceClient.GetBlobContainerClient(sourceContainer);
                BlobContainerClient targetContainerClient = serviceClient.GetBlobContainerClient(targetContainer);
                BlobClient sourceBlobClient = sourceContainerClient.GetBlobClient(blobName);
                BlobClient targetBlobClient = targetContainerClient.GetBlobClient(blobName);
                Console.WriteLine("Sending copy blob request....");
                var result = await targetBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
                Console.WriteLine("Copy blob request sent....");
                Console.WriteLine("============");
                bool isBlobCopiedSuccessfully = false;
                do
                {
                    Console.WriteLine("Checking copy status....");
                    var targetBlobProperties = await targetBlobClient.GetPropertiesAsync();
                    Console.WriteLine($"Current copy status = {targetBlobProperties.Value.CopyStatus}");
                    if (targetBlobProperties.Value.CopyStatus == CopyStatus.Pending)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    else
                    {
                        isBlobCopiedSuccessfully = targetBlobProperties.Value.CopyStatus == CopyStatus.Success;
                        break;
                    }
                } while (true);
    
                if (isBlobCopiedSuccessfully)
                {
                    Console.WriteLine("Blob copied successfully. Now deleting source blob...");
                    await sourceBlobClient.DeleteAsync();
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢!这行得通。有什么方法可以复制和删除还是我们需要编写另一个函数来从以前的容器中删除文件
    • 用删除 blob 代码更新了我的答案。您可以检查 blob 是否复制成功,然后删除源 blob。 HTH。
    猜你喜欢
    • 2016-11-29
    • 2020-07-21
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多