Skip to content

Commit b289d29

Browse files
committed
Add option to connect to blob storage via connection string
1 parent af2ed78 commit b289d29

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

backend/api/Services/BlobService.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ bool overwrite
3131
);
3232
}
3333

34-
public class BlobService(ILogger<BlobService> logger) : IBlobService
34+
public class BlobService(ILogger<BlobService> logger, IConfiguration configuration)
35+
: IBlobService
3536
{
3637
public async Task<byte[]?> DownloadBlob(
3738
string blobName,
@@ -114,12 +115,32 @@ public async Task UploadJsonToBlob(
114115

115116
private BlobContainerClient GetBlobContainerClient(string containerName, string accountName)
116117
{
117-
var credential = new DefaultAzureCredential();
118+
var authType =
119+
configuration[$"BlobStorage:{accountName}:AuthType"]
120+
?? configuration["BlobStorage:DefaultAuthType"]
121+
?? throw new ConfigException(
122+
$"BlobStorage:DefaultAuthType is required (no per-account AuthType found for '{accountName}')"
123+
);
124+
125+
BlobServiceClient serviceClient;
126+
if (authType.Equals("ConnectionString", StringComparison.OrdinalIgnoreCase))
127+
{
128+
var connectionString =
129+
configuration[$"BlobStorage:{accountName}:ConnectionString"]
130+
?? throw new ConfigException(
131+
$"BlobStorage:{accountName}:ConnectionString is required when AuthType is ConnectionString"
132+
);
133+
serviceClient = new BlobServiceClient(connectionString);
134+
}
135+
else
136+
{
137+
var credential = new DefaultAzureCredential();
138+
serviceClient = new BlobServiceClient(
139+
new Uri($"https://{accountName}.blob.core.windows.net"),
140+
credential
141+
);
142+
}
118143

119-
var serviceClient = new BlobServiceClient(
120-
new Uri($"https://{accountName}.blob.core.windows.net"),
121-
credential
122-
);
123144
var containerClient = serviceClient.GetBlobContainerClient(
124145
containerName.ToLower(CultureInfo.CurrentCulture)
125146
);

backend/api/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"Instance": "https://login.microsoftonline.com",
1212
"AllowUsingClientSecret": false
1313
},
14+
"BlobStorage": {
15+
"DefaultAuthType": "DefaultAzureCredential"
16+
},
1417
"Echo": {
1518
"BaseUrl": "https://echohubapi.equinor.com/api",
1619
"Scopes": [

0 commit comments

Comments
 (0)