PowerShell Basics: How to Upload Files to Azure Storage

In recent days there has been a push to move our team's in-person presentations online.  In some cases we've been asked to record our presentations.  has quickly become an issue and so I have been investigating ways to transfer the recorded presentations to the cloud.  Now the transfer can take place via GUI however automating the transfer might be needed in future.  Luckily uploading files to Azure via PowerShell is an option.  

Lets get started:

  1. Run PowerShell as Administrator
     
  2. Install the Azure PowerShell Module via the following command:
    Install-Module -Name Az -AllowClobber 
  3.  

  4. Run the following script to transfer a specified file to Azure :
    $StorageURL = "https://<storagename>.blob.core.windows.net/STORAGE_CONTINER/"
    $FileName = "<filename>"
    $SASToken = "st=2020-03-10T23%3A19%3A17Z&se=2020-03-11T23%3A19%3A17Z&sp=rl&sv=2018-03-28&sr=b&sig=RANDOMCHARS"
    
    $blobUploadParams = @{
        URI = "{0}/{1}?{2}" -f $StorageURL, $FileName, $SASToken
        Method = "PUT"
        Headers = @{
            'x-ms-blob-type' = "BlockBlob"
            'x-ms-blob-content-disposition' = "attachment; filename=`"{0}`"" -f $FileName
            'x-ms-meta-m1' = 'v1'
            'x-ms-meta-m2' = 'v2'
        }
        Body = $Content
        Infile = $FileToUpload
    }

    NOTE: Be sure to replace <storagename> found in line 1 and <filename> found in line 2.

As always, please share your comments below on bettering the above script or any questions you may have.

 

This article was originally published by Microsoft's Entra (Azure AD) Blog. You can find the original article here.