Search code examples
azureazure-blob-storagediskazure-vm

How to directly upload an azure VM disk to a blob storage?


I would like to export an azure vm disk directly to an azure blob storage? I have checked on the internet but i couldn't find a way.

I can download the disk to my local PC and then upload to blob storage. But it takes long time and manual work. So i am checking a more direct way. Maybe with Azure Storage Explorer? The disk and blob storage are in the same region.

Because there is a SAS download link of vm disk, i can paraphrase my question as:

Is there a way to transfer a file directly to blob storage by using a url?

Do you have any suggestions?


Solution

  • If you know the Azure disk's SAS download link, we can use Azure CLI command az storage blob copy start or command-line utility AzCopy to upload the disk as vhs file to Azure blob storage.

    For example

    az login
    
    #Provide the name of your resource group where managed disk is created
    resourceGroupName=myResourceGroupName
    
    #Provide the managed disk name 
    diskName=myDiskName
    
    #Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
    #Know more about SAS here: https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
    sasExpiryDuration=3600
    
    #Provide storage account name where you want to copy the underlying VHD file of the managed disk. 
    storageAccountName=mystorageaccountname
    
    #Name of the storage container where the downloaded VHD will be stored
    storageContainerName=mystoragecontainername
    
    #Provide the key of the storage account where you want to copy the VHD 
    storageAccountKey=mystorageaccountkey
    
    #Provide the name of the destination VHD file to which the VHD of the managed disk will be copied.
    destinationVHDFileName=myvhdfilename.vhd
    
    #Generate the SAS for the managed disk 
    sas=$(az disk grant-access --resource-group $resourceGroupName --name $diskName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)
    
    #Copy the VHD of the managed disk to the storage account
    az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas
    
    # check copy status
    az storage blob show -n $destinationVHDFileName -c $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey