Search code examples
azurepowershellazure-storage-account

upload files to storage account without SAS


I need to upload files to an storage account without using SAS.

I create an "app registration" and give contributor access to the storage account.

If I want to upload files from powershell? How can I do it?

First az login? and then azcopy? Because I tried this way but ask me for a token


Solution

  • The Azure Powershell, Azure CLI and AzCopy are three different things, you should not mix them together.

    If you want to use powershell to upload file with the service principal, after you create the App Registration, please get values for signing in, then create a new application secret.

    In your storage account, the Contributor role is enough, but you should note, actually the Contributor does not have the permission to access the blob directly, it just let you get the context of the storage account, then use the context to access the blob, to access the blob directly, we need the Storage Blob Data Owner/Contributor as mentioned in the comment.

    Then use the script below(the Get-Credential in another reply is an interactive way, here is a non-interactive way, usually, we use the service principal in a non-interactive way for the automation)

    $azureAplicationId ="<Application-ID>"
    $azureTenantId= "<Tenant-ID>"
    $azurePassword = ConvertTo-SecureString "<Application-secret>" -AsPlainText -Force
    $psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
    Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal
    
    $context = (Get-AzStorageAccount -ResourceGroupName <group-name> -Name <storageaccount-name>).Context
    Set-AzStorageBlobContent -Container <container-name> -File <localfile-path> -Blob <blob-name> -Context $context