是否可以在连接字符串中为Azurite模拟器定义辅助存储帐户端点



我正在从Azure Storage Emulator迁移到Azurite,使用此选项可以定义自定义存储帐户(和关联密钥(。

然而,在这样做的过程中,我遇到了与应用程序中的代码和规定的连接字符串格式不兼容的问题(可能是.NET Core 3.1.x的错误或限制(。

我们的存储客户端代码看起来是这样的:

private CloudBlobClient ServiceClient
{
get
{
if (_serviceClientBacking == null)
{
var options = _optionsResolver.Get();
var connectionString = GetStorageConnectionString(options.AzureStorageName, options.AzureStorageKey);
var account = CloudStorageAccount.Parse(connectionString);
_serviceClientBacking = account.CreateCloudBlobClient();
_serviceClientBacking.DefaultRequestOptions = new BlobRequestOptions
{
RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
LocationMode = LocationMode.PrimaryThenSecondary,
MaximumExecutionTime = TimeSpan.FromSeconds(20)
};
}
return _serviceClientBacking;
}
}

传入这样的连接字符串(引用(:

return $"DefaultEndpointsProtocol=http;AccountName={azureStorageName};AccountKey={azureStorageKey};BlobEndpoint=http://{localUrl}:10000/{azureStorageName};QueueEndpoint=http://{localUrl}:10001/{azureStorageName};TableEndpoint=http://{localUrl}:10002/{azureStorageName};";

解析出有效的存储上下文,但是,客户端没有定义辅助连接。如果使用PrimaryThenSecondary模式,客户端代码将阻塞该连接字符串,因为LocationMode实现抛出错误。当更改为使用PrimaryOnly模式时,连接将按预期工作。

这个问题的理想解决方案是一种允许定义辅助连接的连接字符串格式(Azurite应该通过在连接uri中添加-secondary来支持这种格式(,但是,我找不到任何对这种格式的引用。目前,我计划通过仅在本地开发环境中使用模拟器时更改LocationMode来解决此问题。

以下代码应该基于已经弃用的SDK:NuGet Gallery | Microsoft.Azure.Storage.Blob请将其升级到新的SDK NuGet Gallery | Azure.Storage.Blobs 12.14.0

您可以参考.net v12 SDK代码示例,了解如何在新SDK中设置辅助位置Uri:将辅助位置Uri设置为BlobClientOptions,然后使用BlobClientOptions对象创建BlobServiceClient。https://learn.microsoft.com/en-us/azure/storage/blobs/storage-create-geo-redundant-storage?tabs=dotnet#understand-样本代码

有关Azurite辅助位置Uri格式,请参阅https://github.com/Azure/Azurite#ra-grs。

顺便说一句,对于Azurite问题,通常你可以在Issues·Azure/Azurite(github.com(中提出问题。所以其他用户也可以看到。

最新更新