powershell - 如何使用 powershell 重命名 blob 文件

标签 powershell blob azure-data-factory

看似简单的任务。我只想重命名一个 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")" }

进一步阅读:Rename Azure Storage Blob using PowerShell

关于powershell - 如何使用 powershell 重命名 blob 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625603/

相关文章:

azure - 使用服务主体身份验证从 azure 函数读取 AD 组

powershell - 如何获取 Azure DSC 配置来读取名称为参数的自动化帐户变量?

java - 如何使用 Blob 对象将大型原始 XML 文件写入 Oracle 数据库?

c# - 从车辆图像中提取车牌

azure - 我们如何在azure数据工厂中使用ssl证书调用rest api?

Azure 数据工厂查询

json - Powershell ConvertTo-Json键小写

.net - 深度复制PSObject

javascript - 如何在 React 中播放 blob 对象的音频?

azure-data-factory - 在 Synapse Pipeline 中写入 SQL 查询查找事件返回错误消息