<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shashank Mishra</title>
    <description>The latest articles on DEV Community by Shashank Mishra (@shashankm28).</description>
    <link>https://dev.to/shashankm28</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F257856%2F22af83f0-1549-4a9d-9bf7-6829ad89f1cc.jpeg</url>
      <title>DEV Community: Shashank Mishra</title>
      <link>https://dev.to/shashankm28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shashankm28"/>
    <language>en</language>
    <item>
      <title>Azure &amp; Python : List container blobs</title>
      <dc:creator>Shashank Mishra</dc:creator>
      <pubDate>Mon, 27 Jul 2020 22:44:27 +0000</pubDate>
      <link>https://dev.to/shashankm28/azure-python-list-container-blobs-5241</link>
      <guid>https://dev.to/shashankm28/azure-python-list-container-blobs-5241</guid>
      <description>&lt;p&gt;Recently, I had come across a project requirement where I had to list all the blobs present in a Storage Account container and store the blob names in a CSV file.&lt;/p&gt;

&lt;p&gt;I would like to share the Python script which I had created for this task keeping this tutorial as simple as possible.&lt;/p&gt;

&lt;p&gt;I would be dividing this tutorial in 4 parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Connection to Azure Storage Container&lt;/li&gt;
&lt;li&gt;Listing container blobs&lt;/li&gt;
&lt;li&gt;Writing the blob names to a CSV file&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python (and PIP)&lt;/li&gt;
&lt;li&gt;A code editor (I use &lt;a href="https://code.visualstudio.com/download"&gt;VS Code&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;A Microsoft Azure account (with storage account created)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some packages/modules which would be required, these can be installed by running the following command on PowerShell, Command Prompt or Terminal (if on a Linux system):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Azure blob storage module | &lt;a href="https://pypi.org/project/azure-storage-blob/"&gt;Read more&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;pip install azure-storage-blob&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Connection to Azure Storage Container
&lt;/h2&gt;

&lt;p&gt;There are many ways to connect to a container. I would be covering connecting using a &lt;em&gt;Connection String&lt;/em&gt;, &lt;em&gt;SAS Token&lt;/em&gt; and &lt;em&gt;SAS URL&lt;/em&gt;. &lt;em&gt;Managed Identity&lt;/em&gt; and &lt;em&gt;Key Vault&lt;/em&gt; connection methods require some configuration on Azure as well which would be beyond the scope of this tutorial (I would discuss it in another tutorial).&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Connection String/SAS Token via Azure Portal
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Connection string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to your storage account via the portal, on the left hand panel scroll down, click on &lt;em&gt;Access keys&lt;/em&gt; and on the right hand side you will find a pair of Account keys and Connection strings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X6bmhY_s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/z8lk1z702wnwb6uanr0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X6bmhY_s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/z8lk1z702wnwb6uanr0e.png" alt="Connection string" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SAS Token/URL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to your storage account via the portal, on the left hand panel scroll down and click on &lt;em&gt;Shared access signature&lt;/em&gt;. You will have to generate the tokens by selecting the appropriate check boxes according to your requirements. See the below screenshots for reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QMuL57Sn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/skpewqs0ctywodhcz1s8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QMuL57Sn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/skpewqs0ctywodhcz1s8.png" alt="SAS URL" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nm3p_3xH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/f2ff3ozhbpdxm40s1438.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nm3p_3xH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/f2ff3ozhbpdxm40s1438.png" alt="SAS URL" width="800" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: The connection string generated here can be also be used. The only difference between this string and the one generated in the above section is that the string (token and URL as well) generated here has an expiry date.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Connection String&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;In line 5 &amp;amp; 6 the code asks for the connection string and the container name respectively. The reason behind is this is that we would want to establish a connection to a particular container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blob_source_service_client = BlobServiceClient.from_connection_string(source_container_connection_string)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above snippet, in &lt;code&gt;blob_source_service_client&lt;/code&gt; the connection instance to the storage account is stored.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source_container_client = blob_source_service_client.get_container_client(source_container_name)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here using the connection instance of the storage account, we are establishing a connection to a specific container and storing the instance as well as returning it via &lt;code&gt;source_container_client&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SAS Token&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;In this the requirements are a but different, along with the SAS Token, the storage account URL would be required as well. This would change the function parameter list as well as the function call in main(). The significant difference would be&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blob_source_service_client = BlobServiceClient(account_url = account_url, credential = token)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SAS URL&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;blob_source_service_client = BlobServiceClient(source_container_sas_token)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The only major difference here is in line 5. We are passing the SAS URL directly to &lt;code&gt;BlobServiceClient&lt;/code&gt;. Rest all is same as in Connection String section.&lt;/p&gt;

&lt;p&gt;Read more at &lt;a href="https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python"&gt;docs.microsoft.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Listing container blobs
&lt;/h2&gt;

&lt;p&gt;In the above section we have seen how to establish and return a connection instance. Let's jump right in to the next section.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Description
&lt;/h3&gt;

&lt;p&gt;This function accepts two arguments, first the connection instance which we had created earlier and secondly the path for which the blobs have to be listed. So, the above function will print the blobs present in the container for a particular given path.&lt;/p&gt;

&lt;p&gt;One important thing to take note of is that &lt;code&gt;source_blob_list&lt;/code&gt; is an iterable object. The exact type is: &lt;code&gt;&amp;lt;iterator object azure.core.paging.ItemPaged&amp;gt;&lt;/code&gt;, and yes, &lt;code&gt;list_blobs()&lt;/code&gt; supports pagination as well.&lt;/p&gt;

&lt;p&gt;In line 8, I am appending the blob names in a list. Now this list would be passed to the &lt;code&gt;create_csv(blob_list)&lt;/code&gt; function. Discussed in the next section.&lt;/p&gt;

&lt;p&gt;If all the container blobs are to be listed then an empty string (i.e. '') can be set to &lt;code&gt;blob_path&lt;/code&gt; or the parameter can itself be omitted and the argument also can be removed from &lt;code&gt;list_blobs()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the blob names to a CSV file
&lt;/h2&gt;

&lt;p&gt;Coming to the last part, this should be relatively simple and self explanatory.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;A new CSV file would be generated at the location of the script, with the following contents,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WsNTOlXN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/egbsxqr75ra6j697lghz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WsNTOlXN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/egbsxqr75ra6j697lghz.PNG" alt="Generated CSV" width="203" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  break;
&lt;/h1&gt;

&lt;p&gt;I have created a script which comprises of all of the above code neatly jammed up.&lt;/p&gt;

&lt;p&gt;Find the script &lt;a href="https://gist.github.com/shashankm28/22bb2a967a74e989b9a3a6a1d172eeba"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope that this tutorial was helpful. Reach out to me/comment below for any suggestions or queries.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;😁&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>python</category>
      <category>azure</category>
    </item>
  </channel>
</rss>
