简体   繁体   中英

Copy Blob from one storage account to another in Azure in c#

I have One blob which contains my users' CVs.

My site is live. Now I want to copy From one blob to another blob with different storage account.

Here is my code to copy blob

        CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("sourceStorageConnectionString"));
        CloudStorageAccount targetStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("targetStorageConnectionString"));

        CloudBlobClient sourceCloudBlobClient = sourceStorageAccount.CreateCloudBlobClient();
        CloudBlobClient targetCloudBlobClient = targetStorageAccount.CreateCloudBlobClient();

        CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference(CloudConfigurationManager.GetSetting("sourceContainer"));
        CloudBlobContainer targetContainer = targetCloudBlobClient.GetContainerReference(CloudConfigurationManager.GetSetting("targetContainer"));
        targetContainer.CreateIfNotExists();

        // Copy each blob
        foreach (IListBlobItem blob in sourceContainer.ListBlobs(useFlatBlobListing: true))
        {

            Uri thisBlobUri = blob.Uri;

            var blobName = Path.GetFileName(thisBlobUri.ToString());
            Console.WriteLine("Copying blob: " + blobName);

            CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
            CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(blobName);

            Task task = TransferManager.CopyAsync(sourceBlob, targetBlob, true /* isServiceCopy */);

        }

but my concern is: If this copy operation is running and one of the Cv is updated by any user then will it be effect on live site or in copy operation?

You're making an async service-side copy ( isServiceCopy = true ). If the source changes during the copy, its ETag changes, which invalidates the copy. You'd then need to re-start the failed copy.

Note: If you take a snapshot prior to the copy, then you can safely copy the snapshot, knowing that its contents cannot change (even if the base blob changes). You'd need to then deal with cleaning up snapshots, but it's a way to get around concurrent-write issues on the source blob (or you can simply retry the copy, as I mentioned).

I posted an answer to a similar question, here .

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