How to check if blob exists in Azure blob container using PowerShell

I have a Windows PowerShell script that uploads a file to my Azure Blob repository. I want the file to load only if it does not already exist in the container.

How to check if blob exists?

I'm tired of using Get-AzureStorageBlob , but if blob does not exist, it returns an error. Should I parse the error message to determine if blob does not exist? That doesn't seem right ...

And Set-AzureStorageBlobContent asks for confirmation when blob exists. Is there a way to automatically answer "No"? This cmdlet does not have -confirm and -force will overwrite the file (which I do not want).

+4
source share
5 answers

The solution is to end the call to Get-AzureStorageBlob in try / catch and catch a ResourceNotFoundException to determine that blob does not exist.

And don't forget -ErrorAction Stopat the end.

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;
}
+7
source

This is a @Chris answer option. Chris used Exceptions and Try / Catch. On large systems, try / catch. This allows an error to be entered into the code to exclude an exception, and the system will cancel the call history by looking for the corresponding catch statement. However, when all the code is in one function, for simplicity, I prefer to check the return values:

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

, Set-AzureStorageBlobContent -Confirm, -WhatIf.

, , blob - ?

, ( ) try/catch Get-AzureStorageBlob .

0

.

$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
    }
}
0
  $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."
     }

script , blob Azure Storage PowerShell

0

All Articles