Home > front end >  Copy blobs between storage containers in different subscriptions in Azure using powershell
Copy blobs between storage containers in different subscriptions in Azure using powershell

Time:10-19

Iam trying to copy blobs created in last 24 hours from one storage account to another storage account which are in different subscriptions using powershell. I wanted to automate this using Azure automation but facing issue while selecting the Subscription context. It allows me to select only one subscription context at any time and since the 'copy' points to different subscriptions, the script is failing. Any input is highly appreciated, below is my script.


Select-AzSubscription -SubscriptionId $AzureSubscriptionId1

# Get Source Storage Account

$SrcStorage = Get-AzStorageAccount -name $SrcStorageAccountName -ResourceGroupName $SrcStorageAccountRG

Select-AzSubscription -SubscriptionId $AzureSubscriptionId2

# Get Target Storage Account
$TgtStorage = Get-AzStorageAccount -name $TgtStorageAccountName -ResourceGroupName $TgtStorageAccountRG

$Days = 1

Get-AzureStorageBlob -Container $SrcStorageContainerName -Context $SrcStorage.Context | Where-Object { $_.LastModified.DateTime -gt ((Get-Date).AddDays(-$Days)) } |  Start-AzureStorageBlobCopy -DestContainer $TgtStorageContainerName -DestContext $TgtStorage.Context

CodePudding user response:

  1. Download AzCopy tool from microsoft website here: https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10

  2. AZCopy supports data copy between local drives, azure blob, and even AWS.

  3. AZCopy has below syntax: azcopy copy [source] [destination] [flags]

  4. Login to AZCopy of the source location.

    azcopy login

  5. Generate SAS token for the destination location (Make sure you give write permissions)

  6. Run the command AZCopy (It's one command on a single line please)

    azcopy cp "https://[source-account].blob.core.windows.net/[container]/[path/to/blob]" "https://[destination-account].blob.core.windows.net/[container]/[path/to/directory]?[SAS]"

CodePudding user response:

I was able to correct my code. The issue was how I was referring to the Storage context and not because of multiple subscriptions. Here is the working code.

Select-AzSubscription -SubscriptionId $Subscription1

# Get Source Storage Account Context

$StorageAccountContext = New-AzureStorageContext -StorageAccountName $SrcStorageAccountName -StorageAccountKey 'ABC'

Select-AzSubscription -SubscriptionId $Subscription2

# Get Target Storage Account Context

$TgtStorageAccountContext = New-AzureStorageContext -StorageAccountName $TgtStorageAccountName -StorageAccountKey 'DEF'
$Days = 2


Get-AzureStorageBlob -Container $SrcStorageContainerName -Context $StorageAccountContext | Where-Object { $_.LastModified.DateTime -gt ((Get-Date).AddDays(-$Days)) } |  Start-AzureStorageBlobCopy -DestContainer $TgtStorageContainerName -DestContext $TgtStorageAccountContext -Force

  • Related