kate
kate

Reputation: 49

C# :Move blobs from one container to another using Azure.Storage.Blobs client library

I am using the recent Azure.Storage.Blobs client library . I have seen many examples for copy and delete in CloudblockBlob Client using the StartCopyAsync() method but for the newer version I am not able to find anything .

I need to move files from one container to another in the same storage account .

This is the old version

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

Upvotes: 1

Views: 4035

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136126

Please try the following code. It makes use of 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();
            }
        }
    }
}

Upvotes: 2

Related Questions