如何使用 PowerShell 检查 Azure blob 容器中是否已存在 blob
How to check if a blob already exists in Azure blob container using PowerShell
我有一个 Windows PowerShell 脚本,可以将文件上传到我的 Azure Blob 存储。
我只希望文件在容器中不存在时才上传。
如何检查 blob 是否已经存在?
我厌倦了使用 Get-AzureStorageBlob 但如果 blob 不存在,它 returns 是一个错误。我应该解析错误消息以确定该 blob 不存在吗?这好像不太对...
并且 Set-AzureStorageBlobContent 要求在 blob 存在时进行确认。有没有办法自动回答 "No" ?此 cmdlet 没有 -confirm 和 -force 会覆盖文件(我不想要)。
没错,Set-AzureStorageBlobContent
既没有-Confirm
标志也没有-WhatIf
标志。
您真的确定要忽略特定 blob 包含某些内容的事实并只是默默地覆盖它的内容吗?
看起来这里唯一可能的(也是非常丑陋的)解决方案是一个 try
/catch
块,里面有 Get-AzureStorageBlob
。
一个解决方案是将对 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 的列表并查找您的文件。
$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
}
}
$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
下载详细脚本
这是@Chris 回答的变体。 Chris 使用了 Exceptions 和 Try/Catch。在较大的系统中 try/catch 很棒。它允许代码深处的错误抛出异常,系统将回溯调用历史以寻找匹配的 catch 语句。但是,当所有代码都在一个函数中时,为简单起见,我更喜欢检查 return 值:
$blob = Get-AzureStorageBlob -Blob $azureBlobName -Container $azureStorageContainer -Context $azureContext -ErrorAction Ignore
if (-not $blob)
{
Write-Host "Blob Not Found"
}
我有一个 Windows PowerShell 脚本,可以将文件上传到我的 Azure Blob 存储。 我只希望文件在容器中不存在时才上传。
如何检查 blob 是否已经存在?
我厌倦了使用 Get-AzureStorageBlob 但如果 blob 不存在,它 returns 是一个错误。我应该解析错误消息以确定该 blob 不存在吗?这好像不太对...
并且 Set-AzureStorageBlobContent 要求在 blob 存在时进行确认。有没有办法自动回答 "No" ?此 cmdlet 没有 -confirm 和 -force 会覆盖文件(我不想要)。
没错,Set-AzureStorageBlobContent
既没有-Confirm
标志也没有-WhatIf
标志。
您真的确定要忽略特定 blob 包含某些内容的事实并只是默默地覆盖它的内容吗?
看起来这里唯一可能的(也是非常丑陋的)解决方案是一个 try
/catch
块,里面有 Get-AzureStorageBlob
。
一个解决方案是将对 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 的列表并查找您的文件。
$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
}
}
$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
下载详细脚本这是@Chris 回答的变体。 Chris 使用了 Exceptions 和 Try/Catch。在较大的系统中 try/catch 很棒。它允许代码深处的错误抛出异常,系统将回溯调用历史以寻找匹配的 catch 语句。但是,当所有代码都在一个函数中时,为简单起见,我更喜欢检查 return 值:
$blob = Get-AzureStorageBlob -Blob $azureBlobName -Container $azureStorageContainer -Context $azureContext -ErrorAction Ignore
if (-not $blob)
{
Write-Host "Blob Not Found"
}