Issue with PowerShell script copying files between blob containers

sai pavan 40 Reputation points
2024-06-19T12:21:43.93+00:00

Hello team,

I am experiencing issues with a PowerShell script that copies files from one blob container to another. Specifically, I am trying to run this script from a function app. Can anyone assist me in resolving this issue?

User's image

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,114 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,223 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,597 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amol Shelar 421 Reputation points
    2024-06-19T12:53:40.5233333+00:00

    Hello @sai pavan ,

    Unfortunately, the script you mentioned is missing in your message. However, I am sharing the script I used to copy files from one blob container to another.

    # Import the Azure PowerShell module
    Import-Module Az
    
    # Define the storage account and containers
    $sourceStorageAccount = "your-source-storage-account"
    $sourceContainer = "source-container"
    $destinationStorageAccount = "your-destination-storage-account"
    $destinationContainer = "destination-container"
    $resourceGroup = "your-resource-group"
    
    # Authenticate to Azure
    Connect-AzAccount
    
    # Retrieve the source and destination storage account keys
    $sourceStorageKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $sourceStorageAccount).Value[0]
    $destinationStorageKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $destinationStorageAccount).Value[0]
    
    # Create storage context for source and destination
    $sourceContext = New-AzStorageContext -StorageAccountName $sourceStorageAccount -StorageAccountKey $sourceStorageKey
    $destinationContext = New-AzStorageContext -StorageAccountName $destinationStorageAccount -StorageAccountKey $destinationStorageKey
    
    # List blobs in the source container
    $blobs = Get-AzStorageBlob -Container $sourceContainer -Context $sourceContext
    
    # Copy each blob to the destination container
    foreach ($blob in $blobs) {
        $sourceUri = $blob.ICloudBlob.Uri.AbsoluteUri
        $destinationBlob = Start-AzStorageBlobCopy -AbsoluteUri $sourceUri -DestContainer $destinationContainer -DestContext $destinationContext
    
        Write-Output "Copying blob: $($blob.Name) to $($destinationContainer)"
    }
    
    Write-Output "Blob copy completed."
    
    
    

    Don't forget to mark my reply as the answer if it resolves your issue.

    Regards,

    Amol Shelar

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.