您检查以下从 Azure 文件共享中获取文件的方法。
1,使用 Azure CLI。见here。
az login #Log in interactively
az storage file download \
--account-name $storageAccountName \
--account-key $storageAccountKey \
--share-name $shareName \
--path "myDirectory/SampleUpload.txt" \
--dest "SampleDownload.txt" \
--output none
如果您想使用服务主体登录。您需要先创建一个服务主体。请参阅以下文档以创建服务主体
Use the portal to create an Azure AD application and service principal.
Create an Azure service principal with the Azure CLI
并且您需要在您的 azure story 帐户的角色分配中添加对服务主体的读取权限。
那你可以login using service principal:
az login --service-principal --username APP_ID --password PASSWORD --tenant TENANT_ID
az storage file download --account-name ...
2、使用Azure powershell:见here。
Connect-AzAccount #Log in interactively
$ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
$file=Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Directory directiry -Path filepath
你也可以sign in using service principal。
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($sp.ApplicationId, $sp.Secret)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId
3、使用 Azure Rest api。见here。
您可以查看以下示例来验证其余 api 调用:
# Variables
$TenantId = "" # Enter Tenant Id.
$ClientId = "" # Enter Service Principal Client Id.
$ClientSecret = "" # Enter Service Principal Client Secret.
$Resource = "https://management.core.windows.net/"
$SubscriptionId = "" # Enter Subscription Id.
$RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$body = "grant_type=client_credentials&client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource"
$Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'
Write-Host "Print Token" -ForegroundColor Green
Write-Output $Token
# Get file
$fileUrl = "https://myaccount.file.core.windows.net/myshare/mydirectorypath/myfile"
$Headers = @{}
$Headers.Add("Authorization","$($Token.token_type) "+ " " + "$($Token.access_token)")
$file= Invoke-RestMethod -Method Get -Uri $fileUrl -Headers $Headers
Write-Host "Print File" -ForegroundColor Green
Write-Output $file
查看详细示例here。