8

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?

Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
  • This question has been resolved, and I added the API reference link. - [BlobClientOptions Class (Azure.Storage.Blobs) - Azure for .NET Developers | Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobclientoptions?view=azure-dotnet) – hiroakit Jul 06 '20 at 07:30

1 Answers1

15

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.

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • How do I know if a retry actually happened? Is there a way to log information when a retry occurred? – eponymous23 Aug 04 '20 at 18:48
  • 1
    as far as I could see there's no "flag" for that: https://github.com/Azure/azure-sdk-for-net/blob/a42b1b682ac01fe7a5a04be54bf5c1096b3ddb1c/sdk/batch/Microsoft.Azure.Batch/tests/UnitTests/RetryPolicyUnitTests.cs You can raise a question in the github, probably a better place for this question – Thiago Custodio Aug 04 '20 at 18:54