10

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.

How do I check if the blob already exists ?

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...

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).

5 Answers 5

18

This is a variant of @Chris's answer. Chris used Exceptions and Try/Catch. In larger systems try/catch is great. It allows an error deep in the code to throw an exception, and the system will backtrack the call history looking for a matching catch statement. However when all the code is in one function, for simplicity, I prefer checking return values:

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

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.

And don't forget the -ErrorAction Stop at 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;
}
2
  • Does this approach download the blob if it exists? Can we just check some property of the blob to see whether it exists?
    – derek
    Sep 23, 2016 at 1:05
  • @derek No,this cmd only lists blobs. The cmd that downloads is: Get-AzureStorageBlobContent.
    – Chris
    Oct 3, 2016 at 14:19
0

That's right, Set-AzureStorageBlobContent doesn't have neither -Confirm flag nor -WhatIf flag.

Are you really sure you want to ignore the fact that particular blob contains something and just overwrite it's content silently?

It looks like that the only one possible (and pretty ugly one) solution here will be a try/catch block with Get-AzureStorageBlob inside.

1
  • Thank you Dmitry, I've added a more elaborate answer with a code sample based on your input.
    – Chris
    Feb 18, 2015 at 17:46
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."
     }

You can download detail script from how to check if a blob exists in Azure Storage using PowerShell

-1

You can get a list of all blobs and look for your file.

$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
    }
}
2
  • Maybe for small containers but listing all files isn't scalable at all.
    – Chris
    Mar 27, 2016 at 11:24
  • In that case move the $blobs assignment into the loop and add a -Blob parameter. But I guess the performance would suffer in most cases.
    – abbgrade
    Mar 27, 2016 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.