Search This Blog

Breaking

Saturday, 26 March 2022

March 26, 2022

Azure -> Java -> How to read a file without downloading present inside a azure container?

 If you want to read a file from a Azure container folder without downloading to it, you can easily do it by using azure API's. The correct way to download a file from azure container folder is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post











Once you have ensured that the container is present, you can use below method:

  public static void readFile(String containerName, String fileName) throws IOException {
        BlobServiceClient blobServiceClient = createBlobServiceClient();
        BlobContainerClient containerClient =blobServiceClient.getBlobContainerClient(containerName);
        BlobClient blob = containerClient.getBlobClient(fileName);
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(blob.openInputStream()));
        String line;
        while ((line = bufferedreader.readLine()) != null)
        {
            System.out.println(line);

        }

    }

Here we are reading the file after converting it into a stream.

March 26, 2022

Azure -> Java -> How to download a file present inside a azure container folder ?

If you want to download a file from a Azure container folder, you can easily do it by using azure API's. The correct way to download a file from azure container folder is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post










Once you have ensured that the container is present, you can use method:

 public static void downloadFile(String containerName, String fileName) {
        String fileFromContainer = "";
        BlobServiceClient blobServiceClient = createBlobServiceClient();
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        for (BlobItem blobItem : containerClient.listBlobs()) {
            if(blobItem.getName().contains("/")) {
                fileFromContainer = StringUtils.substringAfterLast(blobItem.getName(), "/");
            } else {
                fileFromContainer = blobItem.getName();
            }

            try {
                if (fileFromContainer.contains(fileName)) {
                    BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());
                    File downloadedFile = new File(downloadedPath + fileFromContainer);
                    System.out.println("\nDownloading blob to\n\t " + downloadedFile);
                    blobClient.downloadToFile(downloadedFile.toString());
                }
            } catch (Exception e) {
            }
        }
    }

downloaded file path will be your local folder path.

static String downloadedPath = "data\\downloaded\\";


March 26, 2022

Azure-> Java-> How to download a file present inside a container?

If you want to download a file from a Azure container, you can easily do it by using azure API's. The correct way to download a file from azure container is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post









Once you have ensured that the container is present, you can use below method:

 public static void downloadFiles(String containerName) {
            BlobServiceClient blobServiceClient = createBlobServiceClient();
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
            for (BlobItem blobItem : containerClient.listBlobs()) {
                BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());
                File downloadedFile = new File(downloadedPath + blobItem.getName());
                System.out.println("\nDownloading blob to\n\t " +  downloadedFile);
                blobClient.downloadToFile(downloadedFile.getPath());
            }
        }

where the downloadedPath is your local path from where you want to store the file.

static String downloadedPath = "data\\downloaded\\";

Once the file is downloaded, you can read the content from it also.
March 26, 2022

Azure- > Java -> How to upload a file inside a container?

 If you want to upload a file inside a Azure container, you can easily do it by using azure API's. The correct way to upload a file to azure container is to ensure always that the container is present or not.

So if you want to check that the container is available or not, please check this post











Once you have ensured that the container is present, you can use method:

blobClient.uploadFromFile(uploadPath + fileName);


this method takes your file path from local.

upload path can be :  static String uploadPath = "data\\upload\\";

where data is a folder in your projecct root directory.

and file will be your file name present inside the path.

The method is :


public static void uploadFile(String containerName, String fileName) {

            BlobServiceClient blobServiceClient = createBlobServiceClient();

            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

            BlobClient blobClient = containerClient.getBlobClient(fileName);

            System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());

            blobClient.uploadFromFile(uploadPath + fileName);

        }


Once this is done, you can ensure by listing files present inside the container and validate that your file is present inside it or not.




Thursday, 17 March 2022

March 17, 2022

Azure- > Java -> How to connect with azure to verify that your container exist or not?

 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.