【问题标题】:How to check if a blob already exists in Azure blob container using PowerShell如何使用 PowerShell 检查 Azure blob 容器中是否已存在 blob
【发布时间】:2017-04-28 07:14:32
【问题描述】:

我有一个将文件上传到我的 Azure Blob 存储的 Windows PowerShell 脚本。 我只希望文件在容器中不存在时才上传。

如何检查 blob 是否已存在?

我厌倦了使用Get-AzureStorageBlob,但如果 blob 不存在,它会返回错误。我应该解析错误消息以确定 blob 不存在吗?这似乎不对...

当 blob 存在时,Set-AzureStorageBlobContent 要求确认。有没有办法自动回答“否”?这个 cmdlet 没有 -confirm 并且 -force 会覆盖文件(我不想要)。

【问题讨论】:

    标签: powershell azure-powershell


    【解决方案1】:

    这是@Chris 答案的变体。 Chris 使用了异常和 Try/Catch。在较大的系统中,try/catch 很棒。它允许代码深处的错误引发异常,系统将回溯调用历史以寻找匹配的 catch 语句。但是,当所有代码都在一个函数中时,为简单起见,我更喜欢检查返回值:

    $blob = Get-AzureStorageBlob -Blob $azureBlobName -Container $azureStorageContainer -Context $azureContext -ErrorAction Ignore
    if (-not $blob)
    {
        Write-Host "Blob Not Found"
    }
    

    【讨论】:

      【解决方案2】:

      一种解决方案是将 Get-AzureStorageBlob 调用包装在 try/catch 中并捕获 ResourceNotFoundException 以确定 blob 没有不存在。

      别忘了最后的-ErrorAction Stop

      try
      {   
          $blob = Get-AzureStorageBlob -Blob $azureBlobName -Container $azureStorageContainer -Context $azureContext -ErrorAction Stop
      }
      catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException]
      {
          # Add logic here to remember that the blob doesn't exist...
          Write-Host "Blob Not Found"
      }
      catch
      {
          # Report any other error
          Write-Error $Error[0].Exception;
      }
      

      【讨论】:

      • 这种方法是否会下载 blob(如果存在)?我们可以只检查 blob 的某些属性以查看它是否存在吗?
      • @derek 不,这个 cmd 只列出 blob。下载的cmd是:Get-AzureStorageBlobContent.
      【解决方案3】:

      没错,Set-AzureStorageBlobContent 既没有-Confirm 标志也没有-WhatIf 标志。

      您确定要忽略特定 blob 包含某些内容的事实,而只是默默地覆盖它的内容吗?

      看起来这里唯一可能的(而且非常丑陋的)解决方案将是一个 try/catch 块,里面有 Get-AzureStorageBlob

      【讨论】:

      • 谢谢 Dmitry,我已经根据您的输入添加了一个更详细的答案,其中包含代码示例。
      【解决方案4】:

      您可以获得所有 blob 的列表并查找您的文件。

      $blobs = Get-AzureStorageBlob -Container $config.ImportContainerName -Context $storageContext
      
      ForEach($file in Get-ChildItem -Path $config.LocalImportPath) {
          $blobName = $config.ImportBlobPrefix + "/" + $file.Name
          $blob = $blobs | Where-Object {$_.Name -eq $blobName}
          if (-not $file.Length -eq $blob.Length) {
              echo "todo upload" $file.Name
          }
      }
      

      【讨论】:

      • 也许对于小型容器,但列出所有文件根本无法扩展。
      • 在这种情况下,将$blobs 赋值移动到循环中并添加-Blob 参数。但我猜在大多数情况下性能会受到影响。
      【解决方案5】:
        $storageAccount = Get-AzureRmStorageAccount -ErrorAction Stop | where-object {$_.StorageAccountName -eq $StorageAccountName}
           If($storageAccount)
           {
              $key = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -name $storageAccount.StorageAccountName -ErrorAction Stop)[0].value
              $storageContext = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $key -ErrorAction Stop
              $storageContainer = Get-AzureStorageContainer -Context $storageContext -ErrorAction Stop | where-object {$_.Name -eq $ContainerName}
              If($storageContainer)
              {
                  $blob = Get-AzureStorageBlob -Context $storageContext -Container $ContainerName -ErrorAction Stop | where-object {$_.Name -eq $BlobName}
                  If($blob)
                  {
                      Write-Host "'$BlobName' blob found."
                  }
                  Else 
                  {
                      Write-Warning "'$BlobName' blob not found."
                  }
              }
              Else 
              {
                  Write-Warning "'$ContainerName' storage container not found."
              }
           }
           Else 
           {
               Write-Warning "'$StorageAccountName' storage account not found."
           }
      

      您可以从how to check if a blob exists in Azure Storage using PowerShell下载详细脚本

      【讨论】:

        猜你喜欢
        • 2020-07-22
        • 1970-01-01
        • 2013-07-05
        • 1970-01-01
        • 2013-04-18
        • 1970-01-01
        • 2021-05-16
        • 2011-02-08
        • 2012-06-17
        相关资源
        最近更新 更多