How to Copy containers from one account to another faster

Viewed 80

I am creating a program that copies a container from one storage account to another. However the container I'm copying has thousands of files and it's taking hours to copy. Is there a way to modify my code to copy the container faster? At the moment, I'm going though the container and copying each blob to the other account in a for-loop.

        public static async Task copyContainerAsync(String containerName, String year, String month, string day)
        {
            String connectionString = "connection string";
            String destConnectionString = "destination connection string";

            var sourceBlobContainerClient = new Azure.Storage.Blobs.BlobContainerClient(connectionString, containerName);
            var sourceBlobs = sourceBlobContainerClient.GetBlobs();
            foreach (var blob in sourceBlobs)
            {
                var storageAccount = CloudStorageAccount.Parse(connectionString);
                var blobClient = storageAccount.CreateCloudBlobClient();
                var sourceFilePath = blob.Name;

                var destContainerName = "destinationContainer/" + year + "/" + month + "/" + 27 + "/" + containerName + "/";
                var destFilePath = "year/month/day/";

                var sourceContainerAlternative = blobClient.GetContainerReference(containerName);
                var destContainer = blobClient.GetContainerReference(destContainerName);

                CloudBlockBlob sourceBlob = sourceContainerAlternative.GetBlockBlobReference(sourceFilePath);
                CloudBlockBlob destBlob = destContainer.GetBlockBlobReference(destFilePath);
                // create the blob client for the destination storage account
                var destStorageAccount = CloudStorageAccount.Parse(destConnectionString);
                var destClient = destStorageAccount.CreateCloudBlobClient();

                // destination container now uses the destination blob client
                destContainer = destClient.GetContainerReference(destContainerName);

                // create a 2 hour SAS token for the source file
                var sas = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                {
                    Permissions = SharedAccessBlobPermissions.Read,
                    SharedAccessStartTime = DateTimeOffset.Now.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.Now.AddHours(2)
                });

                // copy to the blob using the 
                destBlob = destContainer.GetBlockBlobReference(blob.Name);
                var sourceUri = new Uri(sourceBlob.Uri + sas);
                await destBlob.StartCopyAsync(sourceUri);

                // copy may not be finished at this point, check on the status of the copy
                while (destBlob.CopyState.Status == Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending)
                {
                    await Task.Delay(1000);
                    await destBlob.FetchAttributesAsync();
                }

                if (destBlob.CopyState.Status != Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Success)
                {
                    throw new InvalidOperationException($"Copy failed: {destBlob.CopyState.Status}");
                }
            }

        }
0 Answers
Related