看似简单的任务。我只想重命名一个 blob 文件,我知道我必须复制它来重命名或执行其他操作,然后删除原始文件,但这很棘手。我已经创建了存储上下文 (New-AzureStorageContext),并获取了 blob (Get-AzureStorageBlob),并找到了 Start-AzureStorageBlobCopy,但如何实际重命名它?
如果可能的话,我也想在同一个容器中执行此操作。理想情况下,我会在 Azure Runbook 中运行它,并使用 Webhook I Azure Data Factory v2 调用它。我确实尝试在 DFv2 的复制作业接收器中使用“添加动态内容”重命名该文件,但我认为您不能。顺便说一句,我只想将日期附加到现有文件名中。谢谢。
最佳答案
您可以使用我的Rename-AzureStorageBlob
便利功能:
function Rename-AzureStorageBlob
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]$Blob,
[Parameter(Mandatory=$true, Position=1)]
[string]$NewName
)
Process {
$blobCopyAction = Start-AzureStorageBlobCopy `
-ICloudBlob $Blob.ICloudBlob `
-DestBlob $NewName `
-Context $Blob.Context `
-DestContainer $Blob.ICloudBlob.Container.Name
$status = $blobCopyAction | Get-AzureStorageBlobCopyState
while ($status.Status -ne 'Success')
{
$status = $blobCopyAction | Get-AzureStorageBlobCopyState
Start-Sleep -Milliseconds 50
}
$Blob | Remove-AzureStorageBlob -Force
}
}
它接受 blob 作为管道输入,因此您可以将 Get-AzureStorageBlob 的结果通过管道传递给它,并且只需提供一个新名称即可:
$connectionString= 'DefaultEndpointsProtocol=https;AccountName....'
$storageContext = New-AzureStorageContext -ConnectionString $connectionString
Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt'|
Rename-AzureStorageBlob -NewName 'MyNewBlob.txt'
要将日期附加到现有文件名,您可以使用以下内容:
Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt' | ForEach-Object {
$_ | Rename-AzureStorageBlob -NewName "$($_.Name)$(Get-Date -f "FileDateTime")" }
关于powershell - 如何使用 powershell 重命名 blob 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625603/