locked
using powershell to copy blob from one container to another container RRS feed

  • Question

  • Team,

    Thanks in advance

    I'm currently writing cmdlet to copy blobs present in one container and this script will run on schedule basis.

    Below code gets all the blobs which are older than 1 days and then copies the data.

    $storageAccountContext = New-AzureStorageContext -StorageAccountName "copyblob" -StorageAccountKey "abc"
    
    $Destinationcontext = New-AzureStorageContext -StorageAccountName 'copyblob1' -StorageAccountKey 'cde'
    
    $Days = 2
    $filelist = Get-AzureStorageBlob -Container 'primary' -Context $storageAccountContext
    foreach ($file in $filelist | Where-Object {$_.LastModified.DateTime -lt ((Get-Date).AddDays(-$Days))})
    {
    
    Start-AzureStorageBlobCopy -SrcContainer 'primary' -SrcBlob $file.name -DestContainer $Name -DestContext $Destinationcontext
    
    }
    

    Below is the error which I'm getting.

    Start-AzureStorageBlobCopy : Could not get the storage context.  Please pass in a storage context or set the current storage context.
    At line:6 char:1
    + Start-AzureStorageBlobCopy -SrcContainer 'primary' -SrcBlob $file.nam ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : CloseError: (:) [Start-AzureStorageBlobCopy], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.StartAzureStorageBlobCopy
     
    

    Friday, August 3, 2018 10:31 AM

Answers

  • Instead of using a foreach loop, pipe the Start-AzureStorageBlobCopy cmdlet to the Get-AzureStorageBlob cmdlet.

    $Container = 'primary'
    $Days = 2
    $StorageAccountContext = New-AzureStorageContext -StorageAccountName 'copyblob' -StorageAccountKey 'abc'
    $Destinationcontext = New-AzureStorageContext -StorageAccountName 'copyblob1' -StorageAccountKey 'cde'
    
    Get-AzureStorageBlob -Container $Container -Context $StorageAccountContext | 
        Where-Object { $_.LastModified.DateTime -lt ((Get-Date).AddDays(-$Days)) } | 
        Start-AzureStorageBlobCopy -DestContainer $Name -DestContext $Destinationcontext

    • Proposed as answer by T_Vuorenmaa Sunday, August 5, 2018 7:04 AM
    • Marked as answer by Qureshi Aquib Monday, August 6, 2018 8:16 AM
    Saturday, August 4, 2018 9:17 PM

All replies

  • Hi, 

    Looks like you dont have -context for the source.

    Try to add -Context $StorageAccountContext like below and see if it works.

    Start-AzureStorageBlobCopy -SrcContainer 'primary' -Context $StorageAccountContext -SrcBlob $file.name -DestContainer $Name -DestContext $Destinationcontext

    Friday, August 3, 2018 6:30 PM
  • no it doesn't work. gives the same error
    Saturday, August 4, 2018 6:24 PM
  • Instead of using a foreach loop, pipe the Start-AzureStorageBlobCopy cmdlet to the Get-AzureStorageBlob cmdlet.

    $Container = 'primary'
    $Days = 2
    $StorageAccountContext = New-AzureStorageContext -StorageAccountName 'copyblob' -StorageAccountKey 'abc'
    $Destinationcontext = New-AzureStorageContext -StorageAccountName 'copyblob1' -StorageAccountKey 'cde'
    
    Get-AzureStorageBlob -Container $Container -Context $StorageAccountContext | 
        Where-Object { $_.LastModified.DateTime -lt ((Get-Date).AddDays(-$Days)) } | 
        Start-AzureStorageBlobCopy -DestContainer $Name -DestContext $Destinationcontext

    • Proposed as answer by T_Vuorenmaa Sunday, August 5, 2018 7:04 AM
    • Marked as answer by Qureshi Aquib Monday, August 6, 2018 8:16 AM
    Saturday, August 4, 2018 9:17 PM
  • Thanks John.
    Monday, August 6, 2018 8:17 AM