gpt4 book ai didi

Azure CLI - 将文件从一个容器前缀复制到另一个容器前缀

转载 作者:行者123 更新时间:2023-12-02 06:01:15 25 4
gpt4 key购买 nike

我有存储帐户“mysa”和容器“mycontainer”。该容器内有两个前缀(“4.1.2.3”和“最新”),其下有文件。我正在尝试将文件从“4.1.2.3”前缀复制到“最新”:

az storage blob copy start-batch --source-container "mycontainer" --pattern "4.1.2.3/*" --destination-container "mycontainer/latest" --account-name mysa

实际结构变为
__ mycontainer
______ 4.1.2.3
______最新
_________ 4.1.2.3

如何使用 Azure CLI 将文件从一个前缀正确移动到另一个前缀?

最佳答案

没有直接的方法将文件从一个容器前缀复制到另一个容器前缀相反,您可以使用下面的 PowerShell 脚本来实现此目的。

谢谢mohit varma你的脚本帮助我找到了解决方案。

$SourceStorageAccount = "source storageaccount"
$SourceStorageKey = "source key"
$DestStorageAccount = "dest storageaccount"
$DestStorageKey = "dest key"

#here added source and destination separately you can use the script for same & differenct container prefix also

$SourceStorageContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
$DestStorageContext = New-AzureStorageContext -StorageAccountName $DestStorageAccount -StorageAccountKey $DestStorageKey

$Containers = Get-AzureStorageContainer -Context $SourceStorageContext
$sourceprefixname ="4.1.2.3"
$destPrefixname = "latest"

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
$folders = New-Object System.Collections.Generic.List[System.Object]

#Do the copy of everything


foreach ($Blob in $Blobs)
{
if($Blob.Name.StartsWith($sourceprefixname))
{
$BlobName = $Blob.Name
$DestBlobNamestr = $BlobName.Replace($sourceprefixname,$destPrefixname)
Write-Output "Copying $BlobName from $ContainerName"
$BlobCopy = Start-CopyAzureStorageBlob -Context $SourceStorageContext -SrcContainer $ContainerName -SrcBlob $BlobName -DestContext $DestStorageContext -DestContainer $ContainerName -DestBlob $DestBlobNamestr
$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
}
}

关于Azure CLI - 将文件从一个容器前缀复制到另一个容器前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com