title: Using Only a SAS Token To Upload in PowerShell
body: <p>I have a <a href="https://docs.microsoft.com/en-us/azure/storage/common/storage-sas-overview" rel="nofollow noreferrer">SAS Token</a> in the form:</p>
  <p><code>https://name.blob.core.windows.net/container?sv=2015-04-05&amp;sr=b&amp;sig=abc123&amp;se=2017-03-07T12%3A58%3A52Z&amp;sp=rw</code></p>
  <p>I am attempting to use an <code>Az</code> Provided Cmdlet in Powershell to upload content to this blob. I am unable to find an API that simply takes the above SAS token and a file to upload.</p>
  <p>Reading this <a href="https://old.reddit.com/r/PowerShell/comments/fgmu9o/outside_of_azcopy_has_anyone_written_any_scripts/" rel="nofollow noreferrer">reddit post</a> it seems like my options are:</p>
  <ol>
  <li>Parse out the <code>StorageAccountName</code> (in the example <code>name</code>), <code>Container</code> (in the example <code>container</code>) and <code>SASToken</code> (in the example above <code>sv=2015-04-05&amp;sr=b&amp;sig=abc123&amp;se=2017-03-07T12%3A58%3A52Z&amp;sp=rw</code>) and then use <code>New-AzStorageContext</code>/<code>Set-AzStorageBlobContent</code>. This more or less is the answer in this StackOverflow Post (<a href="https://stackoverflow.com/questions/50428001/connect-to-azure-storage-account-using-only-sas-token">Connect to Azure Storage Account using only SAS token?</a>)</li>
  <li>Use <code>Invoke-WebRequest</code> or its kin to basically perform the REST call myself.</li>
  </ol>
  <p>I would like to use as many <code>Az</code> provided cmdlets possible so starting with option 1, there doesn't seem to be an API to parse this, the closest I can find is this StackOverflow Post (<a href="https://stackoverflow.com/questions/42646997/using-sas-token-to-upload-blob-content">Using SAS token to upload Blob content</a>) talking about using <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.blob.cloudblockblob?view=azure-dotnet-legacy" rel="nofollow noreferrer">CloudBlockBlob</a>, however it is unclear if this class is available to me in PowerShell.</p>
  <p>To these ends I've created a Regex that appears to work, but is most likely brittle, is there a better way to do this?</p>
  <pre><code>$SASUri = https://name.blob.core.windows.net/container?sv=2015-04-05&amp;sr=b&amp;sig=abc123&amp;se=2017-03-07T12%3A58%3A52Z&amp;sp=rw
  $fileToUpload = 'Test.json'
  
  $regex = [System.Text.RegularExpressions.Regex]::Match($SASUri, '(?i)\/+(?&lt;StorageAccountName&gt;.*?)\..*\/(?&lt;Container&gt;.*)\?(?&lt;SASToken&gt;.*)')
  $storageAccountName = $regex.Groups['StorageAccountName'].Value
  $container = $regex.Groups['Container'].Value
  $sasToken = $regex.Groups['SASToken'].Value
  
  $storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -SasToken $sasToken
  Set-AzStorageBlobContent -File $fileToUpload -Container $container -Context $storageContext -Force
  </code></pre>
  <h2>To Restate The Questions</h2>
  <ol>
  <li>Is there an <code>Az</code> Cmdlet that takes the SAS URI and SAS Token to allow upload?</li>
  <li>(If not) Is there an API to parse the SAS URI + SAS Token?</li>
  </ol>