How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly?

Viewed 6171

Using the latest (12.3.0 at the time of writing) Nuget package for the Azure.Storage.Blobs assembly, and uploading asynchronously with the BlobServiceClient class, I want to set retry options in case of transient failure.

But no overload of the UploadAsync() method takes any object with retry options:

UploadAsync(Stream, BlobHttpHeaders, IDictionary<String,String>, BlobRequestConditions, IProgress<Int64>, Nullable<AccessTier>, StorageTransferOptions, CancellationToken)

And although when creating a BlobServiceClient, it is possible to set BlobClientOptions, and these do inherit a RetryOptions field from the abstract base class ClientOptions, this field is read only:

    // Summary:
    // Gets the client retry options.
    public RetryOptions Retry { get; }

How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly?

1 Answers

You should specify the retry part when creating the blob client. Here's a sample:

    var options = new BlobClientOptions();
    options.Diagnostics.IsLoggingEnabled = false;
    options.Diagnostics.IsTelemetryEnabled = false;
    options.Diagnostics.IsDistributedTracingEnabled = false;
    options.Retry.MaxRetries = 0;

    var client = new BlobClient(blobUri: new Uri(uriString:""), options: options);

In addition, it is possible to set the BlobClientOptions when creating a BlobServiceClient:

var blobServiceClient = new BlobServiceClient
(connectionString:storageAccountConnectionString, options: options );

You can then use BlobServiceClient.GetBlobContainerClient(blobContainerName:"") and BlobContainerClient.GetBlobClient(blobName:"") to build the blob URI in a consistent manner, with options.

Related