{"id":5391,"date":"2017-09-19T19:46:46","date_gmt":"2017-09-19T19:46:46","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5391"},"modified":"2017-09-19T19:46:46","modified_gmt":"2017-09-19T19:46:46","slug":"using-azure-blob-storage","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-azure-blob-storage\/","title":{"rendered":"Using Azure Blob Storage"},"content":{"rendered":"<p>Blob storage has the concept of containers (which can be thought of as directories) and those containers can contain BLOB&#8217;s. Containers <strong>cannot<\/strong> contain containers and hence differ from a file system in structure. <\/p>\n<p>Containers can be private or allow anonymous read access to BLOBs only or allow anonymous read access for containers and BLOBs.<\/p>\n<p>In the Azure Portal, if you haven&#8217;t already got one set up, create a storage account then create a Blob service. Next create a container, mine&#8217;s named <em>test<\/em>. Next, upload a file, I&#8217;ve uploaded <em>Hello World.txt<\/em> which as you imagine simply has the line <em>Hello World<\/em> within it.<\/p>\n<p>When you create a Blob service you&#8217;re assigned an endpoint name, along the lines of https:\/\/&lt;storage account&gt;.blob.core.winodws.net\/<\/p>\n<p>When we add containers, these get the URL <em>https:\/\/&lt;storage account&gt;.blob.core.windows.net\/&lt;container name&gt;<\/em> and files get the URL <em>https:\/\/&lt;storage account&gt;.blob.core.windows.net\/&lt;container name&gt;\/&lt;file name&gt;<\/em><\/p>\n<p>A comprehensive document on using .NET to interact with the Blob storage can be found at <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/storage\/blobs\/storage-dotnet-how-to-use-blobs\" target=\"_blank\">Get started with Azure Blob storage using .NET<\/a>. <\/p>\n<p><strong>Reading a Blob<\/strong><\/p>\n<p>Here&#8217;s some code to read our uploaded Hello World.txt file<\/p>\n<p><em>Firstly we need to use NuGet to add package WindowsAzure.Storage and Microsoft.WindowsAzure.ConfigurationManager.<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.Azure;\r\nusing Microsoft.WindowsAzure.Storage;\r\n\r\nprivate static void ReadBlob()\r\n{\r\n   var storageAccount = CloudStorageAccount.Parse(\r\n      CloudConfigurationManager.GetSetting(\r\n         &quot;StorageConnectionString&quot;));\r\n\r\n   var blobClient = storageAccount.CreateCloudBlobClient();\r\n   var container = blobClient.GetContainerReference(&quot;test&quot;);\r\n   var blob = container.GetBlockBlobReference(&quot;Hello World.txt&quot;);\r\n   var contents = blob.DownloadText();\r\n\r\n   Console.WriteLine(contents);\r\n}\r\n<\/pre>\n<p>In the example our file is a text file, but we can also access the blob as a stream using DownloadToStream (plus there&#8217;s a whole bunch of other methods for accessing the blobs).<\/p>\n<p><strong>Writing a Blob<\/strong><\/p>\n<p>We can write blobs pretty easily also<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static void WriteBlob()\r\n{\r\n   var storageAccount = CloudStorageAccount.Parse(\r\n      CloudConfigurationManager.GetSetting(\r\n         &quot;StorageConnectionString&quot;));\r\n\r\n   var blobClient = storageAccount.CreateCloudBlobClient();\r\n\r\n   var container = blobClient.GetContainerReference(&quot;test&quot;);\r\n\r\n   var blob = container.GetBlockBlobReference(&quot;new.txt&quot;);\r\n   blob.UploadFromFile(&quot;new.txt&quot;);\r\n}\r\n<\/pre>\n<p>In this example, as you&#8217;ll see, we still do the standard, connect to cloud via the blob client, get a reference to the container we want to interact with, but next we get a blob to a file (in this case new.txt didn&#8217;t exist) and then upload or write from a stream to blob storage. If &#8220;new.txt&#8221; does exist in the blob storage it&#8217;ll simply be overwritten.<\/p>\n<p><strong>Deleting a Blob<\/strong><\/p>\n<p>We&#8217;ve looked at Creation\/Update of blobs and Retrieval or them so let&#8217;s complete CRUD operations on blobs with delete<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static void DeleteBlob()\r\n{\r\n   var storageAccount = CloudStorageAccount.Parse(\r\n      CloudConfigurationManager.GetSetting(\r\n         &quot;StorageConnectionString&quot;));\r\n\r\n   var blobClient = storageAccount.CreateCloudBlobClient();\r\n\r\n   var container = blobClient.GetContainerReference(&quot;test&quot;);\r\n\r\n   var blob = container.GetBlockBlobReference(&quot;new.txt&quot;);\r\n   blob.DeleteIfExists();\r\n}\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p>Along with the standard CRUD type operations we can carry out actions on containers, list blobs etc.<\/p>\n<p>See <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/storage\/blobs\/storage-dotnet-how-to-use-blobs\" target=\"_blank\">Get started with Azure Blob storage using .NET<\/a> for more information.<\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/storageservices\/blob-service-rest-api\" target=\"_blank\">Blob Service REST API<\/a> lists the REST API&#8217;s if you&#8217;d prefer to bypass the Microsoft libraries for accessing the Blobs (or need to implement in a different language).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blob storage has the concept of containers (which can be thought of as directories) and those containers can contain BLOB&#8217;s. Containers cannot contain containers and hence differ from a file system in structure. Containers can be private or allow anonymous read access to BLOBs only or allow anonymous read access for containers and BLOBs. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[96],"tags":[],"class_list":["post-5391","post","type-post","status-publish","format-standard","hentry","category-azure-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5391"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5391\/revisions"}],"predecessor-version":[{"id":5407,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5391\/revisions\/5407"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}