Currently, I am working on a project where we are migrating to the Azure cloud. To test some of the functionality with automation, I need to connect my java code to azure so that I can view, upload
and download all the files from the container.
To perform this activity, first I created a trial account in Azure. After that, I created a container inside my azure account.
Now, My use case was to access the container first with java. To perform this operation, I first need to add a jar :
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.15.0</version>
</dependency>
With this dependency, I will be able to access the storage/ container inside the azure cloud.
Once I added this jar, the second step was to create a connection with Azure.
For this, we can create a connection string first:
String connectStr = "DefaultEndpointsProtocol="+DEFAULT_PROTOCOL+";AccountName="+ ACCOUNT_NAME+";AccountKey="+YOUR_KEY+";EndpointSuffix="+END_POINT_SUFFIX+"";
in this, following detail you can fill, as per your credentials:
public static final String ACCOUNT_NAME = "javacodevalidationdeepak";
public static final String DEFAULT_PROTOCOL = "https";
public static final String END_POINT_SUFFIX = "core.windows.net";
Apart fromt this, you will need account key, which you can find from azure.
Once your connection string is ready, you can create a connection with azure with this:
String connectStr = createConnectionString();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
return blobServiceClient;
Once the connection is built, you can access the containers and files present inside it with various already available APIs.
For Example, if you need to check, that the container which you have created is availabe or not, you can use below code sinppet:
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
if (containerClient.exists()) {
System.out.println("Container " + containerName + " is available");
} else {
System.out.println("Container " + containerName + " does not exist.");
}
Similarly, if you need to list down all files inside the container, read the particular file or download a particular file, you can use various Microsoft APIs for that.
I will share further, how to upload and download files from the azure container
in the next articles.
Till then, keep learning.
No comments:
Post a Comment