问题
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).
回答1:
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"
}
回答2:
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;
}
回答3:
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.
回答4:
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
}
}
回答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."
}
You can download detail script from how to check if a blob exists in Azure Storage using PowerShell
来源:https://stackoverflow.com/questions/28570535/how-to-check-if-a-blob-already-exists-in-azure-blob-container-using-powershell