{"id":36609,"date":"2021-06-30T07:51:27","date_gmt":"2021-06-30T02:21:27","guid":{"rendered":"https:\/\/techmasala.addastudents.com\/dev\/?p=36609"},"modified":"2024-01-20T10:08:59","modified_gmt":"2024-01-20T04:38:59","slug":"how-to-download-azure-blob-file-using-sas-token","status":"publish","type":"post","link":"https:\/\/techmasala.addastudents.com\/dev\/how-to-download-azure-blob-file-using-sas-token\/","title":{"rendered":"How to download Azure Blob file using SAS token?"},"content":{"rendered":"\n<p><strong>Azure Blob storage<\/strong><\/p>\n\n\n\n<p>Azure Blob Storage is Microsoft&#8217;s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that doesn&#8217;t adhere to a particular data model or definition, such as text or binary data.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Azure<\/strong> <strong>Blob storage resources<\/strong><\/h5>\n\n\n\n<p>Azure Blob storage offers three types of resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<strong>storage account<\/strong><\/li>\n\n\n\n<li>A&nbsp;<strong>container<\/strong>&nbsp;in the storage account<\/li>\n\n\n\n<li>A&nbsp;<strong>blob<\/strong>&nbsp;in a container<\/li>\n<\/ul>\n\n\n\n<p>The following diagram shows the relationship between these resources.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"111\" src=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-9.png\" alt=\"\" class=\"wp-image-36610\" srcset=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-9.png 331w, https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-9-300x101.png 300w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Shared access signatures (SAS) token<\/h5>\n\n\n\n<p>\u00a0The SAS token is a string that you generate on the client side, for example by using one of the Azure Storage client libraries. The SAS token is not tracked by Azure Storage in any way. You can create an unlimited number of SAS tokens on the client side. After you create a SAS, you can distribute it to client applications that require access to resources in your storage account.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Flowchart :<\/strong><\/h5>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"206\" src=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-10.png\" alt=\"\" class=\"wp-image-36611\" srcset=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-10.png 602w, https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-10-300x103.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Sample C# Code for generating SAS token:<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>private static string GenearteSas(string container,string blob, string accountName, string accountKey)\n        {\n\n            var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);\n            var cloudBlobClient = account.CreateCloudBlobClient();\n            var containerr = cloudBlobClient.GetContainerReference(container);\n            var blobb = containerr.GetBlockBlobReference(blob);\n\n            Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new Azure.Storage.Sas.BlobSasBuilder()\n\n            {\n                BlobContainerName =container,\n                BlobName =blob,\n                ExpiresOn = DateTime.UtcNow.AddMinutes(5),\/\/Let SAS token expire after 5 minutes.\n            };\n            blobSasBuilder.SetPermissions(Azure.Storage.Sas.BlobSasPermissions.Read);\/\/User will only be able to read the blob and it's properties\n            var sasToken = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(accountName, accountKey)).ToString();\n\n        }\n<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong> <a href=\"https:\/\/myaccount.blob.core.windows.net\/pictures\/profile.jpg?{{SAStoken}}\" target=\"_blank\" rel=\"noopener\">https:\/\/myaccount.blob.core.windows.net\/pictures\/profile.jpg?{{SAStoken}}<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"77\" src=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-11.png\" alt=\"\" class=\"wp-image-36612\" srcset=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-11.png 602w, https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2021\/06\/image-11-300x38.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Sample Code reference for other languages:<\/strong><\/h5>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/eventhub\/generate-sas-token\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/rest\/api\/eventhub\/generate-sas-token<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure Blob storage Azure Blob Storage is Microsoft&#8217;s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that doesn&#8217;t adhere to a particular data model or definition, such as text or binary data. Azure Blob storage resources Azure Blob storage offers three types [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":36611,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[764,70],"tags":[767],"class_list":["post-36609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-azure","category-learn-technology-free","tag-azure-cloud-aws-cloudcomputing-microsoft-devops-technology-linux-cybersecurity-programming-it-sas-token-blob"],"_links":{"self":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/36609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/comments?post=36609"}],"version-history":[{"count":4,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/36609\/revisions"}],"predecessor-version":[{"id":37314,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/36609\/revisions\/37314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media\/36611"}],"wp:attachment":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media?parent=36609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/categories?post=36609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/tags?post=36609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}