我有一个设置为私有的天蓝色 blob 容器。我想使用 PowerShell 下载此容器中的文件。
这就是我所说的,但是它每次都给我 ResourceNotFound 错误。即使我放了 -Credential 和我的用户名/访问密钥。当我将容器切换到公共访问时,它始终有效。所以我错过了什么吗?
Invoke-WebRequest -Uri $uri -OutFile $filePath
Run Code Online (Sandbox Code Playgroud)
使用Invoke-WebRequest类似于在浏览器中打开链接。这是从 Azure 存储下载文件的合法方式,但是要做到这一点,您需要在 URI 中包含SAS(共享访问签名),您必须在代码中使用它之前生成它。实现这一目标的 PowerShell 是:
#Download via URI using SAS
$BlobUri = 'https://yourstorageaccount.blob.core.windows.net/yourcontainer/yourfile.txt'
$Sas = '?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D'
$OutputPath = 'C:\Temp\yourfile.txt'
$FullUri = "$BlobUri$Sas"
(New-Object System.Net.WebClient).DownloadFile($FullUri, $OutputPath)
Run Code Online (Sandbox Code Playgroud)
或者,如果您安装了 Azure PowerShell 模块,则可以在没有任何额外痛苦的情况下执行此操作:
# Download via Azure PowerShell
$StorageAccountName = 'yourstorageaccount'
$StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
$StorageContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey.Primary
$FileName = 'yourfile.txt'
$OutputPath = 'C:\Temp'
$ContainerName = 'yourcontainer'
Get-AzureStorageBlobContent -Blob $FilebName -Container $ContainerName -Destination $OutputPath -Context $StorageContext
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8822 次 |
| 最近记录: |