powershell - Azure 自动化、PowerShell 用于获取私有(private) blob 容器中的文件

标签 powershell azure

我有一个设置为私有(private)的 azure blob 容器。我想使用 PowerShell 下载此容器中的文件。

这是我输入的内容,但是每次都会给我 ResourceNotFound 错误。即使我输入 -Credential 和我的用户名/访问 key 。当我将容器切换到公共(public)访问时,它总是有效。那么我错过了什么吗?

Invoke-WebRequest -Uri $uri -OutFile $filePath

最佳答案

使用Invoke-WebRequest类似于在浏览器中打开链接。这是从 Azure 存储下载文件的合法方式,但是为此,您需要 URI 包含 SAS (Shared Access Signature) ,您必须先生成它,然后才能在代码中使用它。实现此目的的 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)

或者,如果您安装了 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

关于powershell - Azure 自动化、PowerShell 用于获取私有(private) blob 容器中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34903180/

相关文章:

Azure Functions 存储帐户 : Used capacity is constantly rising

c# - 无论我做什么,TimerTrigger Azure Functions 都无法工作

powershell - 如何在PowerShell中将绝对路径转换为相对路径?

azure - 如何更新所有 Azure Powershell Az 模块?

带有空格的Powershell打开文件路径

Azure 搜索服务搜索整数

azure - terraform:仅将公共(public)IP添加到一个azure VM

windows - 使用脚本在 Powershell 命令提示符中填写多个答案

powershell - 如何查找用户使用了哪个 Cmdlet 别名?

windows - IPv4 属性 UI 中与 "Use the following IP Address"等效的 Powershell 是什么?