本文介绍了如何使用PowerShell检查Azure Blob容器中是否已存在Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows PowerShell脚本,该脚本将文件上传到我的Azure Blob存储.我只希望文件在容器中尚不存在的情况下上传.

I have a Windows PowerShell script that uploads a file to my Azure Blob Storage.I want the file only to upload if it doesn't already exists in the container.

如何检查blob是否已存在?

How do I check if the blob already exists ?

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

I tired to use Get-AzureStorageBlob but if the blob doesn't exists, it returns an error. Should I parse the error message to determine that the blob doesn't exists ? This doesn't seem right...

Set-AzureStorageBlobContent 要求在blob进行确认时存在.有没有一种方法可以自动回答否"?此cmdlet没有-confirm,-force会覆盖文件(我不想要).

And Set-AzureStorageBlobContent is asking for a confirmation when the blob exists. Is there a way to automatically answer "No" ? This cmdlet doesn't have -confirm and -force would overwrite the file (which I don't want).

推荐答案

一种解决方案是在try/catch中包装对 Get-AzureStorageBlob 的调用,并捕获 ResourceNotFoundException 确定该blob不存在.

A solution is to wrap the call to Get-AzureStorageBlob in a try/catch and catch ResourceNotFoundException to determine that the blob doesn't exist.

也不要忘记最后的-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;
}

这篇关于如何使用PowerShell检查Azure Blob容器中是否已存在Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:11