How to upload a static file in azure blob from grails application
Posted By : Chetan Hallan | 30-Sep-2015
In the previous blog we learnt about what is azure blob and how can we create our blob in azure. If you missed that or haven't read it then refer to my previous blog How to create an azure blob from grails application
In this blog we will learn about how we can upload our static file in azure.
Pre-requisite:-
1. You have read the previous blog and have created a blob successfully.
Code that will help you to upload static file in azure :-
public void upload(){
try
{
System.out.println("upload started");
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference("catalog");
// Define the path to a local file.
final String filePath = "xyz.txt";
// Create or overwrite the "File" blob with contents from a local file.
CloudBlockBlob blob = container.getBlockBlobReference("xyz.txt");
File source = new File(filePath);
blob.getProperties().setContentType("text/html");
blob.upload(new FileInputStream(source), source.length());
System.out.println("Browse url--");
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
In this code i have uploaded a static file named as "xyz.txt" you can change the file name as per your need and "storageConnectionString" will be created as before.
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=mentionYourAccountName;" +
"AccountKey=mentionYourAccountKeyHere";
For further queries don't hesitate to comment 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.