How to create an azure blob from grails application
Posted By : Chetan Hallan | 30-Sep-2015
In this blog we will learn about the Azure Blobs. Azure Blob is a service of storing large amounts of unstructured data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world, or to store application data privately.
Use of Blob storage are as follows :-
- Serving images or documents directly to a browser.
- Storing files for distributed access.
- Streaming video and audio.
- Performing secure backup and disaster recovery.
- Storing data for analysis by an on-premises or Azure-hosted service
How to create a blob in azure storage?
Pre-requisites :-
- Create an azure storage account.
- After creation of account. You will get Account name and Account Key.
- Download Jar azure-storage-2.0.0.jar and build it in your build path. Jar is not version specific you can use any Jar of any version.
Code to create a blob in azure storage is as follows :-
public void create(){
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("catalog");
// Create the container if it does not exist.
container.createIfNotExists();
System.out.println("created");
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
After that you will need to mention the "storageConnectionString" which is the major string to create a connection to our blob in azure this helps in creation a connection to our azure account.
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=mentionYourAccountName;" +
"AccountKey=mentionYourAccountKeyHere";
with the help of this above example you will able to create azure blob with the name "catalog".
If you find any issues/Queries don't hesitate to ask ur queries in the comment section below.
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chetan Hallan
Chetan is a bright Full Stack Java Developer, he has expertise in JAVA, Spring, Hibernate, AngularJS and MySQL.