-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathFactory.cs
315 lines (271 loc) · 10.6 KB
/
Factory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using Azure.Core;
using Azure.Identity;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using FluentStorage.ConnectionString;
using FluentStorage.Azure.Blobs;
namespace FluentStorage {
/// <summary>
/// Blob storage factory
/// </summary>
public static class Factory {
/// <summary>
/// Register Azure module.
/// </summary>
public static IModulesFactory UseAzureBlobStorage(this IModulesFactory factory) {
return factory.Use(new Module());
}
/// <summary>
/// Connect to local emulator
/// </summary>
public static IAzureBlobStorage AzureBlobStorageWithLocalEmulator(this IBlobStorageFactory factory) {
var credential = new StorageSharedKeyCredential(
"devstoreaccount1",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
var client = new BlobServiceClient(
new Uri("http://127.0.0.1:10000/devstoreaccount1"),
credential);
return new AzureBlobStorage(client, "devstoreaccount1", credential);
}
/// <summary>
///
/// </summary>
public static IAzureBlobStorage AzureBlobStorageWithSharedKey(this IBlobStorageFactory factory,
string accountName,
string key,
Uri serviceUri = null) {
if (accountName is null)
throw new ArgumentNullException(nameof(accountName));
if (key is null)
throw new ArgumentNullException(nameof(key));
var credential = new StorageSharedKeyCredential(accountName, key);
var client = new BlobServiceClient(serviceUri ?? GetServiceUri(accountName), credential);
return new AzureBlobStorage(client, accountName, credential);
}
/// <summary>
///
/// </summary>
public static IAzureDataLakeStorage AzureDataLakeStorageWithSharedKey(this IBlobStorageFactory factory,
string accountName,
string key,
Uri serviceUri = null) {
if (accountName is null)
throw new ArgumentNullException(nameof(accountName));
if (key is null)
throw new ArgumentNullException(nameof(key));
var credential = new StorageSharedKeyCredential(accountName, key);
var client = new BlobServiceClient(serviceUri ?? GetServiceUri(accountName), credential);
return new AzureDataLakeStorage(client, accountName, credential);
}
/// <summary>
/// Create Azure Blob Storage with AAD authentication
/// </summary>
/// <param name="factory"></param>
/// <param name="accountName"></param>
/// <param name="tenantId"></param>
/// <param name="applicationId"></param>
/// <param name="applicationSecret"></param>
/// <param name="activeDirectoryAuthEndpoint"></param>
/// <returns></returns>
public static IAzureBlobStorage AzureBlobStorageWithAzureAd(this IBlobStorageFactory factory,
string accountName,
string tenantId,
string applicationId,
string applicationSecret,
string activeDirectoryAuthEndpoint = "https://login.microsoftonline.com/") {
if (accountName is null)
throw new ArgumentNullException(nameof(accountName));
if (tenantId is null)
throw new ArgumentNullException(nameof(tenantId));
if (applicationId is null)
throw new ArgumentNullException(nameof(applicationId));
if (applicationSecret is null)
throw new ArgumentNullException(nameof(applicationSecret));
if (activeDirectoryAuthEndpoint is null)
throw new ArgumentNullException(nameof(activeDirectoryAuthEndpoint));
// Create a token credential that can use our Azure Active
// Directory application to authenticate with Azure Storage
TokenCredential credential =
new ClientSecretCredential(
tenantId,
applicationId,
applicationSecret,
new TokenCredentialOptions() { AuthorityHost = new Uri(activeDirectoryAuthEndpoint) });
// Create a client that can authenticate using our token credential
var client = new BlobServiceClient(GetServiceUri(accountName), credential);
return new AzureBlobStorage(client, accountName);
}
/// <summary>
/// Create Azure Data Lake Gen 2 Storage with AAD authentication
/// </summary>
/// <param name="factory"></param>
/// <param name="accountName"></param>
/// <param name="tenantId"></param>
/// <param name="applicationId"></param>
/// <param name="applicationSecret"></param>
/// <param name="activeDirectoryAuthEndpoint"></param>
/// <returns></returns>
public static IAzureDataLakeStorage AzureDataLakeStorageWithAzureAd(this IBlobStorageFactory factory,
string accountName,
string tenantId,
string applicationId,
string applicationSecret,
string activeDirectoryAuthEndpoint = "https://login.microsoftonline.com/") {
if (accountName is null)
throw new ArgumentNullException(nameof(accountName));
if (tenantId is null)
throw new ArgumentNullException(nameof(tenantId));
if (applicationId is null)
throw new ArgumentNullException(nameof(applicationId));
if (applicationSecret is null)
throw new ArgumentNullException(nameof(applicationSecret));
if (activeDirectoryAuthEndpoint is null)
throw new ArgumentNullException(nameof(activeDirectoryAuthEndpoint));
// Create a token credential that can use our Azure Active
// Directory application to authenticate with Azure Storage
TokenCredential credential =
new ClientSecretCredential(
tenantId,
applicationId,
applicationSecret,
new TokenCredentialOptions() { AuthorityHost = new Uri(activeDirectoryAuthEndpoint) });
// Create a client that can authenticate using our token credential
var client = new BlobServiceClient(GetServiceUri(accountName), credential);
return new AzureDataLakeStorage(client, accountName);
}
/// <summary>
///
/// </summary>
public static IAzureBlobStorage AzureBlobStorageWithTokenCredential(this IBlobStorageFactory factory,
string accountName,
TokenCredential tokenCredential) {
var client = new BlobServiceClient(GetServiceUri(accountName), tokenCredential);
return new AzureBlobStorage(client, accountName);
}
/// <summary>
///
/// </summary>
/// <param name="factory"></param>
/// <param name="sas"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IAzureBlobStorage AzureBlobStorageWithSas(this IBlobStorageFactory factory,
string sas, BlobClientOptions options = default) {
TryParseSasUrl(sas, out string accountName, out string containerName, out string sasQuery);
var client = new BlobServiceClient(new Uri(sas), options);
return new AzureBlobStorage(client, accountName, containerName: containerName);
}
/// <summary>
/// Creates Azure Blob Storage with Managed Identity
/// </summary>
/// <param name="factory"></param>
/// <param name="accountName"></param>
/// <param name="clientId"></param>
/// <returns></returns>
public static IAzureBlobStorage AzureBlobStorageWithMsi(this IBlobStorageFactory factory,
string accountName,
string clientId = null) {
TokenCredential credential = new ManagedIdentityCredential(clientId, null);
var client = new BlobServiceClient(GetServiceUri(accountName), credential);
return new AzureBlobStorage(client, accountName);
}
/// <summary>
/// Creates Azure Data Lake Gen 2 Storage with Managed Identity
/// </summary>
/// <param name="factory"></param>
/// <param name="accountName"></param>
/// <param name="clientId"></param>
/// <returns></returns>
public static IAzureDataLakeStorage AzureDataLakeStorageWithMsi(this IBlobStorageFactory factory,
string accountName,
string clientId = null) {
TokenCredential credential = new ManagedIdentityCredential(clientId, null);
var client = new BlobServiceClient(GetServiceUri(accountName), credential);
return new AzureDataLakeStorage(client, accountName);
}
/*
/// <summary>
/// Creates a blob storage implementation based on Microsoft Azure Blob Storage using development storage.
/// </summary>
/// <param name="factory">Reference to factory</param>
/// <returns>Generic blob storage interface</returns>
public static IAzureBlobStorage AzureBlobDevelopmentStorage(this IBlobStorageFactory factory)
{
return AzureUniversalBlobStorageProvider.CreateForLocalEmulator();
}
}
*/
/// <summary>
/// Create connection string for azure blob storage
/// </summary>
public static StorageConnectionString ForAzureBlobStorageWithSharedKey(this IConnectionStringFactory factory,
string accountName,
string accountKey) {
var cs = new StorageConnectionString(KnownPrefix.AzureBlobStorage);
cs.Parameters[KnownParameter.AccountName] = accountName;
cs.Parameters[KnownParameter.KeyOrPassword] = accountKey;
return cs;
}
/// <summary>
/// Create connection string for azure blob storage
/// </summary>
public static StorageConnectionString ForAzureDataLakeStorageWithSharedKey(this IConnectionStringFactory factory,
string accountName,
string accountKey) {
var cs = new StorageConnectionString(KnownPrefix.AzureDataLakeGen2);
cs.Parameters[KnownParameter.AccountName] = accountName;
cs.Parameters[KnownParameter.KeyOrPassword] = accountKey;
return cs;
}
/// <summary>
///
/// </summary>
public static StorageConnectionString ForAzureBlobStorageWithAzureAd(this IConnectionStringFactory factory,
string accountName,
string tenantId,
string applicationId,
string applicationSecret) {
var cs = new StorageConnectionString(KnownPrefix.AzureBlobStorage);
cs.Parameters[KnownParameter.AccountName] = accountName;
cs.Parameters[KnownParameter.TenantId] = tenantId;
cs.Parameters[KnownParameter.ClientId] = applicationId;
cs.Parameters[KnownParameter.ClientSecret] = applicationSecret;
return cs;
}
/// <summary>
///
/// </summary>
public static StorageConnectionString ForAzureDataLakeStorageWithAzureAd(this IConnectionStringFactory factory,
string accountName,
string tenantId,
string applicationId,
string applicationSecret) {
var cs = new StorageConnectionString(KnownPrefix.AzureDataLakeGen2);
cs.Parameters[KnownParameter.AccountName] = accountName;
cs.Parameters[KnownParameter.TenantId] = tenantId;
cs.Parameters[KnownParameter.ClientId] = applicationId;
cs.Parameters[KnownParameter.ClientSecret] = applicationSecret;
return cs;
}
private static Uri GetServiceUri(string accountName) {
return new Uri($"https://{accountName}.blob.core.windows.net/");
}
private static bool TryParseSasUrl(string url, out string accountName, out string containerName, out string sas) {
try {
var u = new Uri(url);
accountName = u.Host.Substring(0, u.Host.IndexOf('.'));
containerName = u.Segments.Length == 2 ? u.Segments[1] : null;
sas = u.Query;
return true;
}
catch {
accountName = null;
containerName = null;
sas = null;
return false;
}
}
}
}