{"id":4215,"date":"2021-09-30T16:30:18","date_gmt":"2021-09-30T16:30:18","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4215"},"modified":"2021-09-30T16:30:18","modified_gmt":"2021-09-30T16:30:18","slug":"download-file-and-upload-to-azure-blob-storage","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4215","title":{"rendered":"Download File and Upload to Azure Blob Storage"},"content":{"rendered":"<p><strong>Requirement<\/strong><br \/>\nLet&#8217;s say we have requirement to download file from website and upload it to Azure Blob Storage. In our case we are using excel file but you can do it for other files as well.<\/p>\n<p><strong>Details<\/strong><br \/>\nFirst we need to setup Storage account and blob container in Azure. Once you have setup it, you need to get following details:<\/p>\n<p>&gt; Connection String: You can navigate to storage account and get connection string like below<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-4217 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount-300x213.png\" alt=\"storageaccount\" width=\"300\" height=\"213\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount-300x213.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount-1024x727.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount-624x443.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/09\/storageaccount.png 1213w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n&gt; Blob Container Name: You need to get name of the blob container<br \/>\n&gt; File Name: Name of the file that you want use for storing<\/p>\n<p>Once we have all above details we can use following code. First we will download data to byte array using Web Client<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nUri _url= new Uri(@&quot;URL of the location from where you want to download file&quot;);\r\nvar _webclient = new WebClient();\r\n\/\/in case you need to pass credentials to website\r\n_webclient.Credentials = new NetworkCredential(&quot;Username&quot;, &quot;Password&quot;);\r\nbyte[] _filedata = _webclient.DownloadData(uri);\r\n\r\n\/\/Once file is downloaded to byte array we can upload file to Azure Blob Storage\r\n var _storageaccount = CloudStorageAccount.Parse(&quot;Your Azure Storage Account Connection String&quot;);\r\n var _cloudblobclient = _storageaccount.CreateCloudBlobClient();\r\n var _cloudblobcontainer = _cloudblobclient.GetContainerReference(&quot;BlobContainerName&quot;);\r\n var _filename = _cloudblobcontainer.GetBlockBlobReference(&quot;Filename.xlsx&quot;);\r\n\/\/setup mime type\r\n _filename.Properties.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;\r\n\r\n  if (!_filename.Exists())\r\n            {\r\n                using (Stream stream = new MemoryStream(_filedata))\r\n                {\r\n                    await _filename.UploadFromStreamAsync(stream);\r\n                }\r\n            }\r\n\r\n<\/pre>\n<p><strong>Check our other blogs for Azure<\/strong><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3689\">Developing Scheduler for Dynamics 365 CE using Azure Function Part 1<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3829\">Moving Azure Function Configuration \u2013 Quick Tip<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3503\">Dynamics 365 CE and Azure Function<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3784\">Cannot connect to your Azure SQL DB logical server- Azure SQL Server<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3716\">Using Configuration with Azure Function<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=4161\">Using App User in KingswaySoft Connection Manager<\/a><\/p>\n<p>Hope it will help someone !!<br \/>\n<strong>Keep learning and Keep Sharing !!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement Let&#8217;s say we have requirement to download file from website and upload it to Azure Blob Storage. In our case we are using excel file but you can do it for other files as well. Details First we need to setup Storage account and blob container in Azure. Once you have setup it, you need to get following details:&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4215\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[491,402,522],"tags":[914,915,917,916],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4215"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4215"}],"version-history":[{"count":5,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4215\/revisions"}],"predecessor-version":[{"id":4222,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4215\/revisions\/4222"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}