Search code examples
azurepowershellazure-blob-storageazure-storage

Merging two azure blobs to single blob using powershell


In azure container there were multiple blobs having same name with different extensions (eg: file_01.txt,file_02.txt,file_03.txt). Can anyone please tell How to concatenate these 3 file contents to sinlge file content (eg:- All_files.txt) & place it in same conatiner using azure powershell script.


Solution

  • Based on your requirement , we have created a PowerShell script to download the blob from you storage account to local & will merge the blobs and upload back to different container.

    Here is the PowerShell script:

    $storagecontext = New-AzStorageContext -StorageAccountName '<storageaccoutName>' -StorageAccountKey '<storageAccountKey>'
    
    $storagelist= Get-AzStorageBlob -Container '<containerName>' -Context $storagecontext
    
    $storagearray =$storagelist
    
    foreach($storagelist in $storagearray)
    {
        
      Get-AzStorageBlobContent -Container testblob -Blob $storagelist.Name -Context $storagecontext -Destination C:\Users\Downloads\
    
        $name = $storagelist.Name
        Get-Content C:\Users\Downloads\$($name) | Add-Content C:\Users\Downloads\Allfile.txt
       
    }
    az storage blob upload --container-name '<newUplodingContainerName>'  -f 'C:\Users\Downloads\Allfile.txt' -n 'allfile.txt' --account-name '<storageAccountName>' --account-key '<storageAccountKey>'
    

    Here is the reference output of the above PowerShell script :

    enter image description here