本文介绍了如何使用Powershell根据大小将文件从一个容器复制到另一个容器同样适用于所有dest容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在azure的存储帐户的blob中有一个容器,其中包含具有不同大小文件的不同文件夹.另一方面,在同一个存储帐户中,我有10个不同的容器.我必须将这些文件从源容器复制到目标10个容器,但条件是文件应平均分配给所有容器.

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)}

推荐答案

以下是执行此操作的Powershell脚本:

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
    }
}

总体概念类似,只需根据Az CLI更改功能aas即可.这是Az CLI中的复制命令

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/zh-cn/cli/azure/storage/blob/copy?view=azure-cli-latest#az-storage-blob-copy-start

希望有帮助.

这篇关于如何使用Powershell根据大小将文件从一个容器复制到另一个容器同样适用于所有dest容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:33