问题描述
我有一个蔚蓝色的Blob容器设置为私有.我想使用PowerShell在此容器中下载文件.
I have an azure blob container set to private. I want to download files in this container using PowerShell.
这就是我的意思,但是每次都会给我ResourceNotFound错误.即使我输入-Credential和我的用户名/访问密钥.当我将容器切换为公共访问权限时,它始终有效.那我有什么想念的吗?
This is what I put, however it is giving me ResourceNotFound error every time. Even when I put -Credential and my user name/access key. When I switch the container to public access, it always works. So am I missing anything?
Invoke-WebRequest -Uri $uri -OutFile $filePath
推荐答案
使用 Invoke-WebRequest
类似于在浏览器中打开链接.这是从Azure存储下载文件的合法方法,但是要做到这一点,您将需要URI包含 SAS(共享访问签名),您必须先生成该代码,然后才能在代码中使用它.实现此目的的PowerShell是:
Using Invoke-WebRequest
is analogous to opening a link in your browser. It's a legitimate way to download files from Azure Storage, however to do that you'll need the URI to include a SAS (Shared Access Signature), which you'll have to have generated before you use it in your code. The PowerShell to achieve this is:
#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)
或者,如果您安装了Azure PowerShell模块,则可以轻松执行以下操作:
Alternatively, if you have the Azure PowerShell module installed, you can do it without any of that added pain:
# 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
这篇关于Azure自动化,PowerShell可在私有Blob容器中获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!