Codementor Events

Developing Applications For Azure Files with Java

Published Mar 17, 2020Last updated Mar 19, 2020
Developing Applications For Azure Files with Java

Azure is ranked as one of the top cloud providers. It is especially popular for managing enterprise-level and hybrid environments. Azure Files is a cloud-based offering that enables file shares. You can use it for lift and shift migrations, application sharing, and as a files server. In this article, you will learn how to connect Java applications to Azure, and how to use basic Azure File functions.

What Is Azure Files?

Azure Files is a file share storage service offered by Azure. It enables you to set up file shares in the cloud or on-premises using API or the Server Message Block (SMB) protocol.

In Azure Files, you can create file shares through the Azure CLI, Powershell, or the built-in console. When using this service, you are charged for storage and access. You can store up to 5 TB of data per file share you create.

To understand how Azure Files is different from Azure’s other service offerings, you can see the chart below:
Screen Shot 2020-03-17 at 17.00.57.png

Azure Files Use Cases

Azure Files is designed to replace or extend existing file shares. These shares can be used for a wide variety of uses, including:

File servers—replaces NAS-attached storage or on-premises storage. You can replicate data with File Sync to Windows Servers for distributed caching of data and increased performance.
Lift and shift migrations—enables file and application migration without modification.
Application shares—you can configure Files as a centralized share of configuration files or other application data. When using Files in this way, you can only access data via SMB.
Monitoring and analytics—you can use to centralize the storage of log files and metrics data. This data can then be accessed by enterprise monitoring and analytics tools.
Development and testing—you can use file shares to create a central repository of utilities and software used during software development.

How to Connect Java Applications to Azure Files storage

Java forms the base of most web applications. Because of this popularity, it’s likely that at some point you will want to know how to connect Java applications to your file storage services.

To get started, you need to download the Azure Storage SDK for Java and the Java Development Kit (JDK). This walkthrough assumes that you have already created a file share with your existing Azure Storage account.

1) Set up your application to use Azure Files
To access Azure Files from your application, you need to set up access to the storage API. You can do this by adding the following code to the top of your application’s Java file.

import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.file.*;

2) Set up an Azure storage connection string
Next, you need to connect to your storage account. This requires configuring a connection string. You can configure this string with the following code:

public static final String storageConnectionString =
    "DefaultEndpointsProtocol=http;" +
    "AccountName={Your account name};" +
    "AccountKey={Your account key}";

3) Connecting to an Azure storage account
With the connection string you created in the previous step, you can now connect to your account. To do this, you’ll use the CloudStorageAccount object and the associated parse method.

When you set this up, you need to do so in a try/catch block. This is because the connection will throw an invalid key exception.

try {
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
} catch (InvalidKeyException invalidKey) {
    // Handle the exception
}

Using Basic Azure File Functions with Java

Once you have connected your application to your file share, you can begin manipulating files. You can also use Java to create new file shares or delete existing file shares. The steps for performing these actions are covered below.

Create a file share
Shares are containers within Azure Files that host your files and directories. The number of shares you can create is limited by the storage capacity of your account. When you want to access these shares, you can do so via an Azure Files client.

To create a client:
CloudFileClient fileClient = storageAccount.createCloudFileClient();

To create a share:
if (share.createIfNotExists()) {
    System.out.println("New share created");
}

Delete a file share
Deleting a share is also done via your created client. To delete a share you need to first retrieve your storage account and then get a reference to your file share. You can connect to the account and create a client as shown above.

To retrieve a reference:
CloudFileShare share = fileClient.getShareReference({Share name});

To delete the share:

   if (share.deleteIfExists()) {
       System.out.println("Share deleted");
   }
} catch (Exception e) {
    e.printStackTrace();
}

Upload a file
To upload a file you need to obtain a reference to the location you want to store the file. Then, using that reference you can upload the file.

To obtain a reference:
CloudFileDirectory rootDir = share.getRootDirectoryReference();

To upload a file:
final String filePath = "{Path}\{File name}";

CloudFile cloudFile = rootDir.getFileReference("{File name}");
cloudFile.uploadFromFile(filePath);

Conclusion

The main use case for Azure Files is to set up your own fileshares operation, which you can use for developing Java applications. The first step in this process is creating a file share with your Azure account.

The next step is connecting your Java apps to Azure Files storage. This is done by setting up your application to use Azure files, setting up an Azure connection string, and then connecting to your Azure storage account. Once this is properly configured, you will be able to use basic Azure File functions with Java.

Discover and read more posts from Leah Fainchtein Buenavida
get started
post commentsBe the first to share your opinion
Show more replies