locked
copying one blob container data into another blob container RRS feed

  • Question

  • i have write a script to copy one blob container data into another blob container
    it copying only blob name and showing completed inside VHDs are not copying can any one please help in script,,,

    Thanks in Advance

     

    • Edited by luckygiri Thursday, July 27, 2017 4:26 AM
    Thursday, July 27, 2017 4:21 AM

Answers

  • Yes, it is possible to get VHDs from one blob container to another blob container.

    Script to copy one blob container data into another blob container contain VHDs:

    #Define the source storage account and context.

    $SourceStorageAccountName = "yoursourcestorageaccount"

    $SourceStorageAccountKey = "Storage key for yoursourcestorageaccount"

    $SrcContainerName = "yoursrccontainername"

    $SourceContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccountName -StorageAccountKey $SourceStorageAccountKey

     

    #Define the destination storage account and context.

    $DestStorageAccountName = "yourdeststorageaccount"

    $DestStorageAccountKey = "Storage key for yourdeststorageaccount"

    $DestContainerName = "destcontainername"

    $DestContext = New-AzureStorageContext -StorageAccountName $DestStorageAccountName -StorageAccountKey $DestStorageAccountKey

     

    #Get a reference to blobs in the source container.

    $blobs = Get-AzureStorageBlob -Container $SrcContainerName -Context $SourceContext

     

    #Copy blobs from one container to another.

    $blobs| Start-AzureStorageBlobCopy -DestContainer $DestContainerName -DestContext $DestContext

    -----------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Tuesday, August 1, 2017 5:38 AM

All replies

  • Script to copy one blob container data into another blob container contain VHDs:

    ### Source VHD URL (West US) - authenticated container ###

    $srcUri = "https://sourcestoragename.blob.core.windows.net/vhds/winvmos20170727113249.vhd"

     

    ### Source Storage Account ###

    $srcStorageAccount = "sourcestoragename"

    $srcStorageKey = "sXLJR9QDyRRfsgdf5vM3rSpusfggZakgJGCREmec97bggfhfdlpMXgo6J4cVqik31aKABQ=="

     

    ### Target Storage Account ###

    $destStorageAccount = "targetstoragename"

    $destStorageKey = "rE19XfO3UbsdfgdgfdstAOLD42+35gWAHu9N0XS5r6Ldk6TDHhWtA1QroHgDLR1yywXA=="

     

    ### Create the source storage account context ###

    $srcContext = New-AzureStorageContext  –StorageAccountName $srcStorageAccount `

                                            -StorageAccountKey $srcStorageKey 

     

    ### Create the destination storage account context ###

    $destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount `

                                            -StorageAccountKey $destStorageKey 

     

    ### Destination Container Name ###

    $containerName = "copiedvhds"

     

    ### Create the container on the destination ###

    New-AzureStorageContainer -Name $containerName -Context $destContext

     

    ### Start the asynchronous copy - specify the source authentication with -SrcContext ###

    $blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `

                                        -SrcContext $srcContext `

                                        -DestContainer $containerName `

                                        -DestBlob "testcopy1.vhd" `

                                        -DestContext $destContext

    -----------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Thursday, July 27, 2017 7:30 AM
  • Thank you pradeep

    what exactly i am trying is
    ex:- storage account 1 contain 3 vhds
    i have created new storage account and trying to copy all vhds into new storage account
    In the script i have passed few parameters to copy the vhds with date
    when i am running that script it only creating name but Vhds are not copying

    if you don't mind can i have your mailID i will share my script


    • Edited by luckygiri Thursday, July 27, 2017 8:54 AM
    Thursday, July 27, 2017 8:44 AM
  • You can share your script here by concealing PII data.

    You can also reach out us via Twitter (https://twitter.com/AzureSupport) with reference to this thread link and share the script.

    -----------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Thursday, July 27, 2017 10:23 AM
  • workflow CPSTG

    {

    Param

        (

            [Parameter(Mandatory=$true)]

            [String] $SourceRGName,

     

            [Parameter(Mandatory=$true)]

            [String] $SourceSTGAccName,

     

            [Parameter(Mandatory=$true)]

            [String] $SourceCNTName,

         

            [Parameter(Mandatory=$true)]

            [String] $DestinationRGName,

     

            [Parameter(Mandatory=$true)]

            [String] $DestinationSTGAccName

        )

     

    "#******************************* Login to Azure Run As Connection ********************************************#"

    $connectionName = "AzureRunAsConnection"

       

    Try

        {

    # Get the connection "AzureRunAsConnection"

           

                $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName        

     

    "Logging in to Azure..."

     

                Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId `

                -ApplicationId $servicePrincipalConnection.ApplicationId `

                -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

        }

     

    Catch

        {

            if (!$servicePrincipalConnection)

                {

                    $ErrorMessage = "Connection $connectionName not found."

                    throw $ErrorMessage

                }

            else

                {

                    Write-Error -Message $_.Exception

                    throw $_.Exception

                    $ErrorMessage = $_.Exception

     

                }

        }  

    "#******************************* Successfully Logged in to Azure Run As Connection ********************************#"

    InLineScript

    {

    ### Source Storage Account Key ###

     

    $srcStorageKey = (Get-AzureRmStorageAccountkey -ResourceGroupName $Using:SourceRGName -Name $Using:SourceSTGAccName).Value[0]

     

    write-output "$srcStorageKey"

     

    ### Target Storage Account key###

    $destStorageKey = (Get-AzureRmStorageAccountkey -ResourceGroupName $Using:DestinationRGName -Name $Using:DestinationSTGAccName).Value[0]

     

    write-output "$destStorageKey"

     

    ### Create the source storage account context ###

    $srcContext = New-AzureStorageContext -StorageAccountName $Using:SourceSTGAccName -StorageAccountKey $srcStorageKey

      

    ### Create the destination storage account context ###

    $destContext = New-AzureStorageContext -StorageAccountName $Using:DestinationSTGAccName -StorageAccountKey $destStorageKey

     

    $gd = Get-Date -Format "dd-mm-yyyy"

    $gd = $gd

     

    ### Create the first container on the destination ###

    New-AzureStorageContainer -Name $Using:SourceCNTName$gd -Context $destContext

     

    #Get a reference to blobs in the source container.

    $blob = Get-AzureStorageBlob -Container $Using:SourceCNTName -Context $srcContext

     

    Write-OutPut $blob.Name

     

    $blob = $blob.Name

    }

     

    Foreach -parallel ($blobcopy in $blob)

    {

        InLineScript

        {

    #Copy blobs from one container to another.

    Start-AzureStorageBlobCopy -DestContainer $Using:SourceCNTName$gd -DestContext $Using:destContext -SrcBlob $using:blobcopy -SrcContainer $Using:SourceCNTName -Context $Using:srcContext

     

        }

    }

    }

    Thursday, July 27, 2017 10:30 AM
  • pradeep can you please check script once can help me out where it's wrong
    Thursday, July 27, 2017 10:31 AM
  • Refer the SO thread, which addressing the similar issue.

    The script which is provide above is using Azure PowerShell, since you have three VHDs you can repeat the same scripting by changing the $secUri.

    OR

    Use Storage Explorer enables you to copy a blob container to the clipboard, and then paste that blob container into another storage account.

    For more details, refer “Azure Storage Explorer – Copy a blob container”.

    -----------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Friday, July 28, 2017 6:26 AM
  • Thank you pradeep


    with url i have already script i able to copy but i am trying to copy with storage (blob) name with out giving url of single VHDS is it possible

    i am trying to automate so i can't use azure Storage Explorer i need to make it through script


    • Edited by luckygiri Monday, July 31, 2017 4:54 AM
    Monday, July 31, 2017 4:51 AM
  • Yes, it is possible to get VHDs from one blob container to another blob container.

    Script to copy one blob container data into another blob container contain VHDs:

    #Define the source storage account and context.

    $SourceStorageAccountName = "yoursourcestorageaccount"

    $SourceStorageAccountKey = "Storage key for yoursourcestorageaccount"

    $SrcContainerName = "yoursrccontainername"

    $SourceContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccountName -StorageAccountKey $SourceStorageAccountKey

     

    #Define the destination storage account and context.

    $DestStorageAccountName = "yourdeststorageaccount"

    $DestStorageAccountKey = "Storage key for yourdeststorageaccount"

    $DestContainerName = "destcontainername"

    $DestContext = New-AzureStorageContext -StorageAccountName $DestStorageAccountName -StorageAccountKey $DestStorageAccountKey

     

    #Get a reference to blobs in the source container.

    $blobs = Get-AzureStorageBlob -Container $SrcContainerName -Context $SourceContext

     

    #Copy blobs from one container to another.

    $blobs| Start-AzureStorageBlobCopy -DestContainer $DestContainerName -DestContext $DestContext

    -----------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Tuesday, August 1, 2017 5:38 AM
  • Thank you pradeep
    Tuesday, August 1, 2017 12:40 PM