Azure Storage move blob to other container

前端 未结 3 942
鱼传尺愫
鱼传尺愫 2021-02-12 11:50

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 t

相关标签:
3条回答
  • 2021-02-12 12:33

    Here is what worked for me (answer edited after better answer by @Deumber was posted):

        public async Task<CloudBlockBlob> Move(CloudBlockBlob srcBlob, CloudBlobContainer destContainer)
        {
            CloudBlockBlob destBlob;
    
            if (srcBlob == null)
            {
                throw new Exception("Source blob cannot be null.");
            }
    
            if (!destContainer.Exists())
            {
                throw new Exception("Destination container does not exist.");
            }
    
            //Copy source blob to destination container
            string name = srcBlob.Uri.Segments.Last();
            destBlob = destContainer.GetBlockBlobReference(name);
            await destBlob.StartCopyAsync(srcBlob);
            //remove source blob after copy is done.
            srcBlob.Delete();
            return destBlob;
        }
    
    0 讨论(0)
  • 2021-02-12 12:37

    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.

    0 讨论(0)
  • 2021-02-12 12:47

    The answer accepted in this question will move the file to your server memory and then upload it from your memory to azure again.

    A better practice could be let all the work on Azure

    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);
    
    0 讨论(0)
提交回复
热议问题