|
6 | 6 | using Autofac.Builder; |
7 | 7 | using Azure.Data.Tables; |
8 | 8 | using Azure.Identity; |
| 9 | +using Azure.Storage.Blobs; |
9 | 10 | using Microsoft.Extensions.Configuration; |
10 | 11 | using Microsoft.Extensions.DependencyInjection; |
11 | 12 | using Microsoft.Extensions.Options; |
12 | 13 | using NuGet.Services.Configuration; |
| 14 | +using NuGet.Services.Storage; |
13 | 15 | using NuGetGallery; |
14 | 16 |
|
15 | 17 | namespace NuGet.Jobs |
@@ -180,6 +182,48 @@ private static CloudBlobClientWrapper CreateCloudBlobClient( |
180 | 182 | requestTimeout); |
181 | 183 | } |
182 | 184 |
|
| 185 | + public static BlobServiceClient CreateBlobServiceClient( |
| 186 | + StorageMsiConfiguration storageMsiConfiguration, |
| 187 | + string storageConnectionString, |
| 188 | + TimeSpan? requestTimeout = null) |
| 189 | + { |
| 190 | + BlobClientOptions blobClientOptions = new BlobClientOptions(); |
| 191 | + if (requestTimeout.HasValue) |
| 192 | + { |
| 193 | + blobClientOptions.Retry.NetworkTimeout = requestTimeout.Value; |
| 194 | + } |
| 195 | + |
| 196 | + if (storageMsiConfiguration.UseManagedIdentity) |
| 197 | + { |
| 198 | + Uri blobEndpointUri = AzureStorage.GetPrimaryServiceUri(storageConnectionString); |
| 199 | + |
| 200 | + if (string.IsNullOrWhiteSpace(storageMsiConfiguration.ManagedIdentityClientId)) |
| 201 | + { |
| 202 | + // 1. Using MSI with DefaultAzureCredential (local debugging) |
| 203 | + return new BlobServiceClient( |
| 204 | + blobEndpointUri, |
| 205 | + new DefaultAzureCredential(), |
| 206 | + blobClientOptions); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + // 2. Using MSI with ClientId |
| 211 | + return new BlobServiceClient( |
| 212 | + blobEndpointUri, |
| 213 | + new ManagedIdentityCredential(storageMsiConfiguration.ManagedIdentityClientId), |
| 214 | + blobClientOptions); |
| 215 | + } |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + // 3. Using SAS token |
| 220 | + // workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373 |
| 221 | + var connectionString = storageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature="); |
| 222 | + |
| 223 | + return new BlobServiceClient(connectionString, blobClientOptions); |
| 224 | + } |
| 225 | + } |
| 226 | + |
183 | 227 | private static TableServiceClient CreateTableServiceClientClient( |
184 | 228 | StorageMsiConfiguration msiConfiguration, |
185 | 229 | string tableStorageConnectionString) |
|
0 commit comments