How to specify retry options (maximum retries etc) when working with blob storage with Azure SDK for Java

688 Views Asked by At

Is there a way to specify the maximum number of retires to "downloadToFile" method (also to "upload" method) in com.microsoft.azure.storage.blob.CloudBlockBlob? Or do we have to implement a max number of retries logic if we are calling that method? or is there any way that Azure SDK for Java implements max retires? I saw this article: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-retry-policy But it is for .NET SDK. How to implement it in Java SDK?

CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient serviceClient = account.createCloudBlobClient();

// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("container-name");
container.createIfNotExists();

// Upload a file.
CloudBlockBlob blob = container.getBlockBlobReference("demo.txt");
File sourceFile = new File("/path/to/file/demo.txt");
try (FileInputStream sourceStream = new FileInputStream(sourceFile)) {
    blob.upload(sourceStream, sourceFile.length());
}

`

Note: dependency and its version I'm using are:

<dependency>    
   <groupId>com.microsoft.azure</groupId>    
   <artifactId>azure-storage</artifactId>    
   <version>8.6.6</version>
   </dependency>
1

There are 1 best solutions below

1
Venkatesan On

I tried in my environment and got the below results:

Is there a way to specify the maximum number of retires to "downloadToFile" method (also to "upload" method) in com.microsoft.azure.storage.blob.CloudBlockBlob

You can use the below code for attempts to upload a blob with retries using a while loop that continues until either the operation succeeds or the maximum number of retries is reached. If a StorageException is thrown, indicating a failure, the code waits for a specified interval before attempting the operation again.

Code:

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 com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.File;
import java.io.FileInputStream;


public class App {

    private static final int MAX_RETRIES = 3;
    private static final int RETRY_INTERVAL_MS = 1000;

    public static void main(String[] args) throws Exception {
        // Parse the connection string and create a client
        String connectionString = "DefaultEndpointsProtocol="Your connection string";
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        CloudBlobClient client = account.createCloudBlobClient();

        // Get a reference to a container and create it if it doesn't exist
        CloudBlobContainer container = client.getContainerReference("test3");
        container.createIfNotExists();

        // Get a reference to a block blob
        CloudBlockBlob blob = container.getBlockBlobReference("sample.txt");

        // Upload a file with retries
        File fileToUpload = new File("C:\\Users\\secret.txt");
        try (FileInputStream stream = new FileInputStream(fileToUpload)) {
            int retries = 0;
            boolean success = false;
            while (!success && retries < MAX_RETRIES) {
                try {
                    blob.upload(stream, fileToUpload.length());
                    success = true;
                } catch (StorageException ex) {
                    retries++;
                    if (retries == MAX_RETRIES) {
                        throw ex;
                    }
                    Thread.sleep(RETRY_INTERVAL_MS);
                }
            }
            System.out.println("The File is uploaded successfully!!!");
        }
    }
}

Output:

The File is uploaded successfully!!!

enter image description here

Portal: enter image description here

For download a blob with max tries, you can use the below code:

Code:

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 com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.File;
import java.io.FileInputStream;


public class App {

    private static final int MAX_RETRIES = 3;
    private static final int RETRY_INTERVAL_MS = 1000;

    public static void main(String[] args) throws Exception {
        // Parse the connection string and create a client
        String connectionString = "Your connection string";
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        CloudBlobClient client = account.createCloudBlobClient();

        // Get a reference to a container and create it if it doesn't exist
        CloudBlobContainer container = client.getContainerReference("test3");
        container.createIfNotExists();

        // Get a reference to a block blob
        CloudBlockBlob blob = container.getBlockBlobReference("sample.txt");

        // Download a file with retries
        File fileToDownload = new File("C:\\Users\\demo.txt");
        int retries = 0;
        boolean success = false;
        while (!success && retries < MAX_RETRIES) {
            try {
                blob.downloadToFile(fileToDownload.getAbsolutePath());
                success = true;
            } catch (StorageException ex) {
                retries++;
                if (retries == MAX_RETRIES) {
                    throw ex;
                }
                Thread.sleep(RETRY_INTERVAL_MS);
            }
        }
        System.out.println("The File is Downloaded successfully!!!");
    }
    
}

Output:

The File is Downloaded successfully!!!

enter image description here

file: enter image description here