How to copy files from one container to another containers fits equally in all dest containers according to size using powershell

≡放荡痞女 提交于 2021-02-11 13:28:56

问题


I have one container in blob of storage account in azure contains different folder having files of different sizes. In other side, in same storage account, I have 10 different containers. I have to copy these files from source container to destination 10 containers but the condition is the files should be equally distributed to all the containers.

I have tried below so far

$key = "abcdxyz" 

# declaring the azure context

$context = New-AzStorageContext -StorageAccountName abcd  -StorageAccountKey $key 

#Getting the data from the blob

$bacdata = Get-AzStorageContainer -Name sourcecontainer*  -Context $context | Get-AzStorageBlob 

$15=$bacdata | where{$_.Name -like "sourcecontainer1*"} | where{$_.LastModified -gt (get-date).adddays(-1)}

回答1:


Here is the powershell script to do it:

#Server side storage copy
$SourceStorageAccount = "sourceAccountName"
$SourceStorageKey = "sourceAccountAPIKey"
$DestStorageAccount = "destinationAccountName"
$DestStorageKey = "destinationAccountAPIKey"
$SourceStorageContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
$DestStorageContext = New-AzureStorageContext -StorageAccountName $DestStorageAccount -StorageAccountKey $DestStorageKey

$Containers = Get-AzureStorageContainer -Context $SourceStorageContext

foreach($Container in $Containers)
{
    $ContainerName = $Container.Name
    if (!((Get-AzureStorageContainer -Context $DestStorageContext) | Where-Object { $_.Name -eq $ContainerName }))
    {   
        Write-Output "Creating new container $ContainerName"
        New-AzureStorageContainer -Name $ContainerName -Permission Off -Context $DestStorageContext -ErrorAction Stop
    }

    $Blobs = Get-AzureStorageBlob -Context $SourceStorageContext -Container $ContainerName
    $BlobCpyAry = @() #Create array of objects

    #Do the copy of everything
    foreach ($Blob in $Blobs)
    {
       $BlobName = $Blob.Name
       Write-Output "Copying $BlobName from $ContainerName"
       $BlobCopy = Start-CopyAzureStorageBlob -Context $SourceStorageContext -SrcContainer $ContainerName -SrcBlob $BlobName -DestContext $DestStorageContext -DestContainer $ContainerName -DestBlob $BlobName
       $BlobCpyAry += $BlobCopy
    }

    #Check Status
    foreach ($BlobCopy in $BlobCpyAry)
    {
       #Could ignore all rest and just run $BlobCopy | Get-AzureStorageBlobCopyState but I prefer output with % copied
       $CopyState = $BlobCopy | Get-AzureStorageBlobCopyState
       $Message = $CopyState.Source.AbsolutePath + " " + $CopyState.Status + " {0:N2}%" -f (($CopyState.BytesCopied/$CopyState.TotalBytes)*100) 
       Write-Output $Message
    }
}

Overall concept would be similar , just change the function aas per the Az CLI. here is the copy command in Az CLI

az storage blob copy start

You can find more details here.

https://docs.microsoft.com/en-us/cli/azure/storage/blob/copy?view=azure-cli-latest#az-storage-blob-copy-start

Hope it helps.



来源:https://stackoverflow.com/questions/56499363/how-to-copy-files-from-one-container-to-another-containers-fits-equally-in-all-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!