Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Storage move blob to other container

Tags:

I'm looking for an approach to move a blob in Azure from one container to another. The only solution I found is to use the Azure Storage Data Movement Library, but this seems to work between different accounts. I would like to move the blob within the same account to an other container.

like image 247
Jan Avatar asked Oct 26 '16 11:10

Jan


People also ask

How do I move a blob from one container to another?

Copy and move blobs from one container or storage account to another from the command line and in code. Use . NET, AzCopy, and Azure CLI to migrate files between Azure storage accounts.

Can blob be moved?

The blob will only move when you press right arrow! Your job will be to modify the computer program to make it move in other directions.

How do I move files in Azure Blob Storage?

Click on the ... to the right of the Files box, select one or multiple files to upload from the file system and click Upload to begin uploading the files. To download data, selecting the blob in the corresponding container to download and click Download.

Which tool can be used to move data between storage accounts in Azure?

AzCopy is a command-line tool for copying data to or from Azure Blob storage, Azure Files, and Azure Table storage, by using simple commands. The commands are designed for optimal performance. Using AzCopy, you can either copy data between a file system and a storage account, or between storage accounts.


1 Answers

I have not used Azure Storage Data Movement Library but I am pretty sure that it will work in the same storage account as well.

Coming to your question, since Move operation is not natively supported by Azure Storage what you can do is implement this by invoking Copy Blob followed by Delete Blob. In general Copy operation is async however when a blob is copied in same storage account, it is a synchronous operation i.e. copy happens instantaneously. Please see sample code below which does just this:

    static void MoveBlobInSameStorageAccount()     {         var cred = new StorageCredentials(accountName, accountKey);         var account = new CloudStorageAccount(cred, true);         var client = account.CreateCloudBlobClient();         var sourceContainer = client.GetContainerReference("source-container-name");         var sourceBlob = sourceContainer.GetBlockBlobReference("blob-name");         var destinationContainer = client.GetContainerReference("destination-container-name");         var destinationBlob = destinationContainer.GetBlockBlobReference("blob-name");         destinationBlob.StartCopy(sourceBlob);         sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);     } 

However, please keep in mind that you use this code only for moving blobs in the same storage account. For moving blobs across storage account, you need to ensure that copy operation is complete before you delete the source blob.

like image 115
Gaurav Mantri Avatar answered Oct 19 '22 23:10

Gaurav Mantri