Azure Storage move blob to other container

前端 未结 3 971
鱼传尺愫
鱼传尺愫 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 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;
        }
    

提交回复
热议问题