azure - 使用 Azure AD 连接到 Azure 存储时重用 TokenCredential
问题描述
我有一个使用 Azure AD 连接到 Azure 存储 Blob 的 Web API 应用程序。
这是使用的库:
这是创建 BlobServiceClient 的代码。这是针对需要访问存储帐户 blob 的每个请求执行的。我需要
var blobClientOptions = new BlobClientOptions();
blobClientOptions.Diagnostics.IsLoggingEnabled = true;
blobClientOptions.Diagnostics.IsTelemetryEnabled = true;
blobClientOptions.Diagnostics.IsDistributedTracingEnabled = false;
blobClientOptions.Retry.Mode = RetryMode.Exponential;
blobClientOptions.Retry.MaxRetries = 5;
var storageAccountUrl = $"https://{_storageAccountName}.blob.core.windows.net";
_blobServiceClient = new BlobServiceClient(new Uri(storageAccountUrl), new DefaultAzureCredential(), blobClientOptions);
我应该缓存和重用 TokenCredential 吗?它有到期时间吗?使用 Azure AD 连接到 Azure 存储帐户时的最佳做法是什么?
解决方案
凭证本身不缓存令牌。在新的 Azure SDK 中,BlobServiceClient(和其他 API 客户端)使用的 HTTP 管道缓存令牌并在需要时从凭据请求新令牌。您可以使用新的凭证或将其缓存,无论哪种方式都有效。我们使用了单例凭证对象。
推荐阅读
- javascript - 新项目进入时将状态更改为true并在5秒后更改为false
- python - TensorFlow WALS 矩阵分解估计器的 export_savedmodel() 如何正确?
- python - 我在扫描字符串文字时遇到语法错误,说 EOL 为什么?
- python - 将 while 循环值插入到 tkinter 魔术窗口
- python - 测试 python 代码时出错:TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
- python - 如何安装在 pycharm explorer 中找不到的 Prject interpeter
- javascript - 如何在 JavaScript 程序中使用来自 python 脚本的一些数据?
- java - Apache HttpClient - 使用 SSL 或 TLSv1.2 时忽略 keepalive
- reactjs - Firebase/React/Redux 组件有奇怪的更新行为,状态应该没问题
- swift - Swift 4 转换错误:无法将“String”类型的值转换为预期的参数类型“NSAttributedStringKey”