如何使用Powershell重命名Blob文件 [英] How to rename a blob file using powershell

查看:38
本文介绍了如何使用Powershell重命名Blob文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看似简单的任务.我只想重命名blob文件,我知道我必须将其复制以重命名或其他内容,然后删除原始文件,但是事实证明这很棘手.我已经创建了存储上下文(New-AzureStorageContext),并获得了blob(Get-AzureStorageBlob),并找到了Start-AzureStorageBlobCopy,但实际上该如何重命名呢?

seemingly simple task. I just want to rename a blob file, I know I have to copy it to rename or something, then delete the original but this is proving tricky. I have created the storage context (New-AzureStorageContext), and got the blob (Get-AzureStorageBlob), and found Start-AzureStorageBlobCopy, but how to I actually rename it?

如果可能的话,我也想在同一个容器中进行此操作.理想情况下,我将在Azure Runbook中运行它,并使用Web挂钩I Azure Data Factory v2对其进行调用.我确实尝试在DFv2的复制作业接收器中使用添加动态内容"来重命名文件,但我认为您不能.顺便说一句,我只想将日期附加到现有文件名上.谢谢.

I'd like to do this within the same container if possible as well. Ideally I'd run it in an Azure Runbook and call it using a webhook I Azure Data Factory v2. I did try to rename the file using 'Add Dynamic Content' in the copy job sink in DFv2, but I don't think you can. By the way, I just want to append the date to the existing file name. Thank you.

推荐答案

您可以使用我的 Rename-AzureStorageBlob 便捷功能:

You can use my Rename-AzureStorageBlobconvenience function:

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的结果传递给它,并提供一个新名称:

It accepts the blob as pipeline input so you can pipe the result of the Get-AzureStorageBlob to it and just provide a new name:

$connectionString= 'DefaultEndpointsProtocol=https;AccountName....'
$storageContext = New-AzureStorageContext -ConnectionString $connectionString

Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt'|
    Rename-AzureStorageBlob -NewName 'MyNewBlob.txt'

要将日期附加到现有文件名中,您可以使用类似以下内容的文件:

To append the date to the existing file name you can use something like:

Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt' | ForEach-Object { 
$_ | Rename-AzureStorageBlob -NewName "$($_.Name)$(Get-Date -f "FileDateTime")" }

进一步阅读:重命名Azure存储使用PowerShell的Blob

这篇关于如何使用Powershell重命名Blob文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆