title: How do I authenticate a user against an Azure storage blob in python?
body: <p>I'm looking for a way to authenticate a user against an Azure blob container. The sample code (yep, newbie alert) works just fine, using an access key for the storage account, but that feels uncomfortably like giving away full control of the entire storage account to anyone who steals the credentials.</p>
  
  <p>The auth sample (from <a href="https://azure.microsoft.com/en-us/resources/samples/storage-python-getting-started/" rel="nofollow noreferrer">https://azure.microsoft.com/en-us/resources/samples/storage-python-getting-started/</a>) looks like this:</p>
  
  <pre><code>block_blob_service = BlockBlobService(account_name='&lt;acc&gt;', account_key='&lt;key&gt;')
  </code></pre>
  
  <p>I have a service user set up in Active Directory with a role in the storage account restricting its use of the blob container; it's intended to do nothing but write new items into one specific container. </p>
  
  <p>I'd like to use that user's credentials in the python script so that if it leaks, there's no access to other storage resources. Is there a way to generate an access key based on a resource/id combination, or similar way to achieve that? I've been browsing the Azure Python API docs, but not making any headway.</p>
  
  <p>Edit: I've made a little progress. I've created a service principal with appropriate IAM restrictions. That appears to log in successfully when I call this:</p>
  
  <pre><code>credentials = ServicePrincipalCredentials( client_id=&lt;&gt;, secret=&lt;&gt;, tenant=&lt;&gt;)
  print(credentials)
  </code></pre>
  
  <p>Which gives me an object:</p>
  
  <pre><code>&lt;msrestazure.azure_active_directory.ServicePrincipalCredentials object at 0x7f34f52668d0&gt;
  </code></pre>
  
  <p>And an error if I give it incorrect credentials. So, great, I have a credentials object. Now what? I can't find a way to feed it into BlockBlobService.</p>