What is Azure functions?
Azure Functions is a serverless compute service that allows you to run event-triggered code without having to explicitly provision or manage infrastructure.
In server-less architecture project code was divided independently running functions. those functions are executed in cloud providers machine when the function is accessed. In usual scenario to deploy applications we’d like buy servers from cloud providers, install OS, runtime etc., even we dont have traffic at starting of our deployment still we’d like buy our server usage without using it. But within the case of serverless architecture we write code as independent functions and store it in cloud storage when the necessity for function execution is raised the cloud provider dynamically allocate server and executes the code. Here we only got to buy code execution time not for the servers.
Azure Functions allows us to run small pieces of code (“functions”) without considering about application infrastructure. With the Azure Functions, the cloud infrastructure provides all the up-to-date servers us need to keep our application running at scale.
- Create storage account
Go to Azure Portal and Go to Storage Accounts and Click on Add.
- When the deployment is complete, click Go to resource.
- Click Containers.
- Click + Container.
- Name the container.
- Select Blob from the drop-down list.
Once you created if you click on the container and go to properties you will get a URL for your Blob container that will be used to link your storage account with spring boot application.
2.Configure spring boot application
- Now we need to link this account in to the spring boot application. To do that need to add Azure Storage Api import to the project in pom.xml file. And import the changes.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.6.5</version>
</dependency>
2.Now we have to get Connection String to the storage account that we created from Azure Portal. Go to the storage account → Click Access Keys . there you can see the connection string we can access our storage account with that string.
3.Copied Connection string should be added to the spring boot application.proporties file as shown below.
azure.storage.ConnectionString= youConnection String here
azure.storage.container.name= Your container name here
3.Code for upload to azure storage
- Bean Configuration for uploading file it is spring boot stuff of creating object for dependency.
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
@Configuration
public class BeanConfig {
@Autowired
private Environment env;
@Bean
public CloudStorageAccount cloudStorageAccount(){
String property = env.getProperty(“azure.storage.ConnectionString”);
CloudStorageAccount account = null;
try {
account = CloudStorageAccount.parse(env.getProperty(“azure.storage.ConnectionString”));
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return account;
}
@Bean
public CloudBlobClient cloudBlobClient(){
return cloudStorageAccount().createCloudBlobClient();
}
@Bean
public CloudBlobContainer cloudBlobContainer(){
String containerName = env.getProperty(“azure.storage.container.name”);
CloudBlobContainer container = null;
try {
container = cloudBlobClient().getContainerReference(containerName);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
}
return container;
}
}
2. Function to upload blob file to Azure storage .Here i’m going to write product details in Text file name the text file with product id and upload it to the Blob Container. You can see the code below.
@SpringBootApplication
public class StorageServiceApplication {
@Autowired
CloudBlobContainer blobContainer;
public static void main(String[] args) {
SpringApplication.run(StorageServiceApplication.class, args);
}
public StorageResponse upload(Product product, CloudBlobContainer cloudBlobContainer){
URI uri = null;
boolean error = false;
CloudBlockBlob blob = null;
try {
blob = cloudBlobContainer.getBlockBlobReference(product.getId()+”.txt”);
blob.uploadText(product.toString());
// blob.upload(new ByteArrayInputStream(multipartFile), -1);
uri = blob.getUri();
} catch (URISyntaxException e) {
error =true;
e.printStackTrace();
} catch (StorageException e) {
error =true;
e.printStackTrace();
}catch (IOException e) {
error =true;
e.printStackTrace();
}
return new StorageResponse(uri.toString(),error, HttpStatus.OK.toString());
}
}
NextTopic:Azure CDN & Azure FrontDoor