使用 SAS 令牌上传 Blob 内容

Using SAS token to upload Blob content

我在使用 Blob SAS 令牌通过 Powershell 将文件写入 Azure 中的 Blob 时遇到困难。

我用来生成 SAS 令牌的代码是:

$storageContext = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageName

$token = New-AzureStorageBlobSASToken -Container $conName -Context $storageContext.Context -Blob $blobName -ExpiryTime $expiry -Permission rw -FullUri

这会按预期生成令牌:https://name.blob.core.windows.net/container/test.json?sv=2015-04-05&sr=b&sig=abc123&se=2017-03-07T12%3A58%3A52Z&sp=rw

如果我在浏览器中使用它,它工作正常并按预期下载文件。但是,我不能用它来上传文件。每当我尝试时,我都会收到 (403) Forbidden。我用来上传的代码是:

$accountContext = New-AzureStorageContext -SasToken $sasToken

Get-AzureStorageContainer -Context $accountContext.Context | Set-AzureStorageBlobContent -File $blobFile

在调用 Add-AzureRmAccount 进行身份验证后,我已经成功地使用了与此类似的方法来设置 Blob 内容。

我也曾尝试使用 Container SAS 令牌,但我一直收到 403 错误。

令牌可用于读取这一事实让我相信我的 Powershell 脚本中遗漏了一些东西 - 任何人都可以阐明那是什么吗?

The fact that the token works for a read leads me to believe that I'm missing something in my Powershell script - can anyone shed any light on what that is?

我认为问题在于以下代码行:

Get-AzureStorageContainer -Context $accountContext.Context

这里有两件事:

  1. 此 cmdlet 尝试列出存储帐户中的 blob 容器。为了使用 SAS 列出 blob 容器,您需要一个 Account SAS,因为您使用的 SAS 是 Container SAS.
  2. 您的 SAS 只有 ReadWrite 权限。要列出容器,您还需要 List 许可。

我建议只使用 Set-AzureStorageBlobContent Cmdlet 并向其提供必要的信息,而不是通过管道获取容器名称。

Set-AzureStorageBlobContent -File $blobFile -Container $conName -Context $accountContext.Context -Blob $blobName