如何使用Azure.Storage.Blobs程序集在Azure Blob存储操作上设置重试策略? [英] How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly?

查看:94
本文介绍了如何使用Azure.Storage.Blobs程序集在Azure Blob存储操作上设置重试策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新的(在撰写本文时为12.3.0 ) Nuget 软件包 Azure.Storage.Blobs 程序集,并使用 BlobServiceClient 类异步上传,我想设置重试选项,以防出现短暂故障.

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.

但没有 UploadAsync()方法的重载接受具有重试选项的任何对象:

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)

尽管创建了 BlobServiceClient 时,也可​​以设置 BlobClientOptions ,并且它们的确从抽象基类继承了 RetryOptions 字段 ClientOptions ,此字段为只读:

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; }

如何使用 Azure.Storage.Blobs 程序集对Azure blob存储操作设置重试策略?

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

推荐答案

在创建Blob客户端时,应指定重试部分.这是一个示例:

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);

此外,可以在创建 BlobServiceClient 时设置 BlobClientOptions :

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

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

然后,您可以使用 BlobServiceClient.GetBlobContainerClient(blobContainerName:") BlobContainerClient.GetBlobClient(blobName:")以一致的方式构建Blob URI,带有选项.

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

这篇关于如何使用Azure.Storage.Blobs程序集在Azure Blob存储操作上设置重试策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆