How to rename the container name in windows azure?

后端 未结 5 2409
北海茫月
北海茫月 2021-02-19 17:08

Is there any way by which we can rename the blob container name in windows azure ?

5条回答
  •  无人共我
    2021-02-19 17:33

    This RenameBlob method will allow you to rename folders or files in an Azure container.

    • album1/image1.jpg -> album2/image1.jpg
    • album1/image1.jpg -> album1/image2.jpg

    HTH

    public class AzureStorageService
    {
        readonly CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
    
        public void RenameBlob(string oldName, string newName)
        {
            var blobClient = _storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("MyContainer");
    
            var blobs = container.GetDirectoryReference(oldName).ListBlobs();
    
            foreach (var item in blobs)
            {
                string blobUri = item.Uri.ToString();
    
                var oldBlob = container.GetBlockBlobReference(blobUri);
                var newBlob = container.GetBlockBlobReference(blobUri.Replace(oldName, newName));
    
                newBlob.StartCopyFromBlob(oldBlob);
    
               oldBlob.Delete();
            }
        }
    }
    

提交回复
热议问题