{"id":35436,"date":"2016-06-08T19:20:43","date_gmt":"2016-06-08T13:50:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=35436"},"modified":"2016-06-09T09:48:46","modified_gmt":"2016-06-09T04:18:46","slug":"introduction-to-microsoft-azure-blob-service","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-microsoft-azure-blob-service\/","title":{"rendered":"Getting Started With Azure Blob Service Using Grails"},"content":{"rendered":"<p>Many of you already know about Microsoft Azure, which is a cloud based computing platform infrastructure for deploying, and managing applications and services globally. One of its most useful service is the blob service, which is a service for storing large amounts of unstructured data, whether text or binary data which is easily accessible from anywhere.<\/p>\n<p><span style=\"font-weight: 400;\">After reading this blog, you\u2019ll be able to perform blob operations such as download, upload and list the blobs from the blob container using Microsoft Azure Blob service.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>Basic Terms of Blob Service:<\/b><\/p>\n<p><b>Blob: <\/b><span style=\"font-weight: 400;\">It stands for &#8220;Binary Large Object.&#8221; Its usage is in database-land, where blob data might be data stored in our database does not confirm to an established data type as defined by the database.<\/span><\/p>\n<p><b>StorageAccount: \u00a0<\/b><span style=\"font-weight: 400;\">A storage account is required to create a blob client and further to perform \u00a0various operations on Blob Storage. <\/span><\/p>\n<p><b>Blob Container<\/b><b>: <\/b><span style=\"font-weight: 400;\">A blob container refers of organization of blobs and it is necessary to create and store a blob.The container has its own metadata and properties. <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span>The first step(s) to use Azure&#8217;s API involve adding\u00a0the dependency to your <a title=\"grails development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails<\/a> app &amp; use the credentials from your Azure account to connect to the API. Basic steps are outlined below:<\/p>\n<p><strong>\u00a0Step 1: \u00a0\u00a0Add Azure SDK\u00a0jar dependency<\/strong><\/p>\n<p>The first step is to add the Azure SDK dependency to your project. The Maven link to the dependency is as follows:<\/p>\n<p>http:\/\/mvnrepository.com\/artifact\/com.microsoft.azure\/azure-core\/0.9.3<\/p>\n<p>If you\u2019re using Gradle, then the dependency declaration in build.gradle is as follows<\/p>\n<p>[code]<br \/>\ndependencies {<br \/>\n  &#8230;<br \/>\n  &#8230;<br \/>\n  compile &#8216;com.microsoft.azure:azure-core:0.9.3&#8217;<br \/>\n  compile &#8216;com.microsoft.azure:azure-storage:4.2.0&#8217;<br \/>\n  &#8230;<br \/>\n  &#8230;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p><b>Step 2: \u00a0\u00a0\u00a0Get Azure credentials<\/b><\/p>\n<p>The next step is to register with an account on Azure so that we can create get the credentials to communicate with Azure\u2019s API. Here is the link \u00a0where you can register<\/p>\n<p><span style=\"font-weight: 400;\">https:\/\/azure.microsoft.com\/en-us\/?c=16.17<\/span><\/p>\n<p>After the successful registration, you\u2019ll get your account name and key .<\/p>\n<p><span style=\"font-weight: 400;\">Put the &#8220;accountName&#8221; and &#8220;accountKey&#8221; \u00a0into <strong>application.yml<\/strong> or <strong>application.groovy <\/strong><\/span><\/p>\n<p>Example:<\/p>\n<p>[code]<br \/>\n&#8212;<\/p>\n<p>cloud:<br \/>\n\tazure:<br \/>\n\t\tcredentials:<br \/>\n\t\t\taccessName: &quot;XXXXXXX&quot;<br \/>\n\t\t\tsecretkey :  &quot;XXXXXXX&quot;<\/p>\n<p>&#8212;<br \/>\n[\/code]<\/p>\n<p>Once you&#8217;re account is setup and all necessary dependencies have been added to your application, we can move towards the more interesting part.\u00a0Let&#8217;s take a quick look at some code samples that can upload, download and list blob objects in a container. As usual, code samples are written for Grails 3.x projects , but it should work as it is for other SpringBoot based frameworks.<\/p>\n<h3><strong>Upload Blob:<\/strong><\/h3>\n<p>Uploading a blob is very simple and is outlined in the following three\u00a0steps:<\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0<strong>Step 1<\/strong>: Get a container reference and use it to get a blob reference.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0<strong>Step 2<\/strong>:\u00a0Call upload on the blob reference.<\/span><\/p>\n<p><strong>\u00a0 \u00a0Step 3<\/strong>: Use the blob reference to upload \u00a0new blob or overwrite an existing one.<\/p>\n<p>Following is a code sample that will let you upload blob objects:<\/p>\n<p>[java]<br \/>\ndef upload() {<\/p>\n<p>\u00a0\u00a0def file = request.getFile(&#8216;filePath&#8217;)<br \/>\n\u00a0\u00a0File source = new File(file.originalFilename)<br \/>\n\u00a0\u00a0source.setBytes(file.getBytes());<\/p>\n<p>\u00a0\u00a0try {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlobContainer container = getContainer()<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlockBlob blob = container.getBlockBlobReference(file.originalFilename);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0blob.upload(new FileInputStream(source), source.length());<\/p>\n<p>  }<\/p>\n<p>\u00a0\u00a0catch (Exception e) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0e.printStackTrace();<br \/>\n\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0forward action: &#8216;list&#8217;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>Connection:<\/strong><\/p>\n<p>[java]<br \/>\nprivate String getConnection() {<br \/>\n\u00a0return &quot;DefaultEndpointsProtocol=http;&quot; +<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&quot;AccountName=${accountName};&quot; +<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&quot;AccountKey=${accessKey}&quot;;<\/p>\n<p>}<\/p>\n<p>private CloudBlobContainer getContainer() {<\/p>\n<p>  CloudStorageAccount storageAccount = CloudStorageAccount.parse(getConnection());<br \/>\n\u00a0\u00a0CloudBlobClient blobClient = storageAccount.createCloudBlobClient();<br \/>\n\u00a0\u00a0CloudBlobContainer container = blobClient.getContainerReference(&quot;testapp&quot;);<br \/>\n\u00a0\u00a0return container<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p><strong>Download Blob<\/strong>:<\/p>\n<p><strong>Step 1<\/strong>: Get a container reference and use it to get a blob reference.<\/p>\n<p><strong>Step 2<\/strong>: call download to transfer the blob contents to a stream object such as a<\/p>\n<p><span style=\"font-weight: 400;\">FileOutputStream that you can use to persist the blob to a local file. <\/span><\/p>\n<p>[java]<br \/>\ndef download(String fileName) {<\/p>\n<p>\u00a0\u00a0try {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlobContainer container = getContainer()<br \/>\n\u00a0 \u00a0 \u00a0\u00a0for (ListBlobItem blobItem : container.listBlobs()) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (blobItem instanceof CloudBlob) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlob blob = (CloudBlob) blobItem;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (blob.getName() == fileName) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0blob.download(new FileOutputStream(&quot;\/home\/sagar\/Desktop\/&quot; + blob.getName()));<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0}<br \/>\n\u00a0\u00a0catch (Exception e) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0e.printStackTrace();<br \/>\n\u00a0\u00a0}<br \/>\n\u00a0\u00a0forward action: &#8216;list&#8217;<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><strong>Lists the Blob<\/strong>:<\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 1<\/strong>: Get a container reference and use it to get a blob reference.\u00a0<\/span><\/p>\n<p><strong>Step 2<\/strong>: then get \u00a0a list blob items from listblobs method of container.<\/p>\n<p>[java]<br \/>\ndef list() {<br \/>\n\u00a0\u00a0List uriList = [];<br \/>\n\u00a0\u00a0try {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlobContainer container = getContainer()<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (ListBlobItem blobItem : container.listBlobs()) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CloudBlob blob = (CloudBlob) blobItem;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uriList.add(blob.getName());<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0}<br \/>\n\u00a0\u00a0catch (Exception e) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0e.printStackTrace();<br \/>\n\u00a0\u00a0}<br \/>\n\u00a0\u00a0render view: &quot;blobOperation&quot;, model: [uriList: uriList]<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><b>Full Example of Azure Blob Storage Service<\/b><span style=\"font-weight: 400;\"><strong> at<\/strong> :<\/span><\/p>\n<p>https:\/\/github.com\/sagarmal624\/AzureBlobStorage.git<\/p>\n<p><strong><br \/>\n<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many of you already know about Microsoft Azure, which is a cloud based computing platform infrastructure for deploying, and managing applications and services globally. One of its most useful service is the blob service, which is a service for storing large amounts of unstructured data, whether text or binary data which is easily accessible from [&hellip;]<\/p>\n","protected":false},"author":926,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7,446,1],"tags":[3457,3462,3461,3465,3466,3458,3464,4840,3459,3460,2072,3463],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35436"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/926"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=35436"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35436\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=35436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=35436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=35436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}