我正在枚举本地文件夹并将其上传到Azure存储。我只想将 new 内容上传到我的Azure存储中。如果我将Set-AzStorageBlobContent与-Force一起使用,它将覆盖所有内容。如果我在没有-Force的情况下使用它,则会提示已存在的项目。我可以使用Get-AzStorageBlob检查该项目是否已经存在,但是如果该项目不存在,则会显示红色错误。我找不到这些项目的组合,这些项目只能优雅地上传新内容,而不会打印任何错误或提示。我使用了错误的方法吗?
最终编辑:根据Ivan Yang的建议添加工作解决方案。现在,仅上传新文件,而没有任何错误消息。关键是使用-Erroraction Stop将错误消息转换为异常,然后捕获该异常。
# In my code this is part of a Test-Blob function that returns $blobFound
$blobFound = $false
try
{
$blobInfo = Get-AzStorageBlob `
-Container $containerName `
-Context $storageContext `
-Blob $blobPath `
-Erroraction Stop
$blobFound = ($null -ne $blobInfo)
}
catch [microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException]
{
# Eat the error that'd otherwise be printed
}
# Note in my code this is actually a call to my Test-Blob function
if ($false -eq $blobFound)
{
Set-AzStorageBlobContent `
-Container $containerName `
-Context $storageContext `
-File $sourcePath `
-Blob $blobPath `
-Force # -Force is unnecessary but just being paranoid to avoid prompts
}