-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
DataLakeUriBuilder.cs
406 lines (365 loc) · 13.7 KB
/
DataLakeUriBuilder.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using Azure.Core;
using Azure.Storage.Sas;
using Azure.Storage.Shared;
namespace Azure.Storage.Files.DataLake
{
/// <summary>
/// The <see cref="DataLakeUriBuilder"/> class provides a convenient way to
/// modify the contents of a <see cref="System.Uri"/> instance to point to
/// different Azure Data Lake resources like an file system, directory, or file.
/// </summary>
public class DataLakeUriBuilder
{
/// <summary>
/// The Uri instance constructed by this builder. It will be reset to
/// null when changes are made and reconstructed when <see cref="System.Uri"/>
/// is accessed.
/// </summary>
private Uri _uri;
/// <summary>
/// Whether the Uri is an IP Uri as determined by
/// <see cref="UriExtensions.IsHostIPEndPointStyle(Uri)"/>.
/// </summary>
private readonly bool _isIPStyleUri;
/// <summary>
/// Gets or sets the scheme name of the URI.
/// Example: "https"
/// </summary>
public string Scheme
{
get => _scheme;
set { ResetUri(); _scheme = value; }
}
private string _scheme;
/// <summary>
/// Gets or sets the Domain Name System (DNS) host name or IP address
/// of a server.
///
/// Example: "account.file.core.windows.net"
/// </summary>
public string Host
{
get => _host;
set { ResetUri(); _host = value; }
}
private string _host;
/// <summary>
/// Gets or sets the port number of the URI.
/// </summary>
public int Port
{
get => _port;
set { ResetUri(); _port = value; }
}
private int _port;
/// <summary>
/// Gets or sets the Azure Storage account name.
/// </summary>
public string AccountName
{
get => _accountName;
set { ResetUri(); _accountName = value; }
}
private string _accountName;
/// <summary>
/// Gets or sets the name of a file storage share. The value defaults
/// to <see cref="string.Empty"/> if not present in the
/// <see cref="System.Uri"/>.
///
/// </summary>
public string FileSystemName
{
get => _fileSystemName;
set { ResetUri(); _fileSystemName = value; }
}
private string _fileSystemName;
/// <summary>
/// Gets or sets the path of the directory or file. The value defaults
/// to <see cref="string.Empty"/> if not present in the
/// <see cref="System.Uri"/>.
/// Example: "mydirectory/myfile"
/// </summary>
public string DirectoryOrFilePath
{
get => _directoryOrFilePath;
set
{
ResetUri();
if (value == null)
{
_directoryOrFilePath = null;
}
else if (value == "/")
{
_directoryOrFilePath = value;
}
else
{
_directoryOrFilePath = value.TrimEnd('/');
}
}
}
private string _directoryOrFilePath;
/// <summary>
/// Gets or sets the name of a file snapshot. The value defaults to
/// <see cref="string.Empty"/> if not present in the <see cref="System.Uri"/>.
/// </summary>
public string Snapshot
{
get => _snapshot;
set { ResetUri(); _snapshot = value; }
}
private string _snapshot;
/// <summary>
/// Gets or sets the Shared Access Signature query parameters, or null
/// if not present in the <see cref="System.Uri"/>.
/// </summary>
public DataLakeSasQueryParameters Sas
{
get => _sas;
set { ResetUri(); _sas = value; }
}
private DataLakeSasQueryParameters _sas;
/// <summary>
/// Get the last directory or file name from the <see cref="DirectoryOrFilePath"/>, or null if
/// not present in the <see cref="Uri"/>.
/// </summary>
internal string LastDirectoryOrFileName =>
DirectoryOrFilePath.TrimEnd('/').Split('/').LastOrDefault();
/// <summary>
/// Gets or sets any query information included in the URI that's not
/// relevant to addressing Azure storage resources.
/// </summary>
public string Query
{
get => _query;
set { ResetUri(); _query = value; }
}
private string _query;
/// <summary>
/// Initializes a new instance of the <see cref="DataLakeUriBuilder"/>
/// class with the specified <see cref="System.Uri"/>.
/// </summary>
/// <param name="uri">
/// The <see cref="System.Uri"/> to a storage resource.
/// </param>
public DataLakeUriBuilder(Uri uri)
{
Scheme = uri.Scheme;
Host = uri.Host;
Port = uri.Port;
AccountName = "";
FileSystemName = "";
DirectoryOrFilePath = "";
Snapshot = "";
Sas = null;
// Find the share & directory/file path (if any)
if (!string.IsNullOrEmpty(uri.AbsolutePath))
{
var path = uri.GetPath();
var startIndex = 0;
if (uri.IsHostIPEndPointStyle())
{
_isIPStyleUri = true;
var accountEndIndex = path.IndexOf("/", StringComparison.InvariantCulture);
// Slash not found; path has account name & no share name
if (accountEndIndex == -1)
{
AccountName = path;
startIndex = path.Length;
}
else
{
AccountName = path.Substring(0, accountEndIndex);
startIndex = accountEndIndex + 1;
}
}
else
{
// DataLake Uris have two allowed subdomains
AccountName = uri.GetAccountNameFromDomain(Constants.DataLake.BlobUriSuffix) ??
uri.GetAccountNameFromDomain(Constants.DataLake.DfsUriSuffix) ??
string.Empty;
}
// Find the next slash (if it exists)
var shareEndIndex = path.IndexOf("/", startIndex, StringComparison.InvariantCulture);
if (shareEndIndex == -1)
{
// Slash not found; path has file system & no directory/file path
FileSystemName = path.Substring(startIndex);
}
else
{
// The file system name is the part between the slashes
FileSystemName = path.Substring(startIndex, shareEndIndex - startIndex);
string directoryOrFilePath = path.Substring(shareEndIndex + 1);
// The directory/file path name is after the share slash
if (directoryOrFilePath.Length == 0)
{
DirectoryOrFilePath = "/";
}
else
{
DirectoryOrFilePath = directoryOrFilePath.UnescapePath();
}
}
}
// Convert the query parameters to a case-sensitive map & trim whitespace
var paramsMap = new UriQueryParamsCollection(uri.Query);
if (paramsMap.TryGetValue(Constants.SnapshotParameterName, out var snapshotTime))
{
Snapshot = snapshotTime;
// If we recognized the query parameter, remove it from the map
paramsMap.Remove(Constants.SnapshotParameterName);
}
if (paramsMap.ContainsKey(Constants.Sas.Parameters.Version))
{
Sas = new DataLakeSasQueryParameters(paramsMap);
}
Query = paramsMap.ToString();
}
/// <summary>
/// Returns the <see cref="System.Uri"/> constructed from the
/// <see cref="DataLakeUriBuilder"/>'s fields. The <see cref="Uri.Query"/>
/// property contains the SAS and additional query parameters.
/// </summary>
public Uri ToUri()
{
if (_uri == null)
{
_uri = BuildUri().ToUri();
}
return _uri;
}
/// <summary>
/// Gets the blob Uri.
/// </summary>
internal Uri ToBlobUri()
{
if (!_isIPStyleUri)
{
string account = UriExtensions.GetAccountNameFromDomain(Host, Constants.DataLake.DfsUriSuffix);
if (account != null)
{
// Replace "dfs" with "blob"
int serviceIndex = Host.IndexOf(Constants.DataLake.DfsUriPeriodSuffix, StringComparison.OrdinalIgnoreCase);
if (serviceIndex > 0)
{
StringBuilder stringBuilder = new StringBuilder(Host);
stringBuilder.Replace(
Constants.DataLake.DfsUriPeriodSuffix,
Constants.DataLake.BlobUriPeriodSuffix,
serviceIndex,
5);
Host = stringBuilder.ToString();
}
}
}
return ToUri();
}
/// <summary>
/// Gets the dfs Uri.
/// </summary>
internal Uri ToDfsUri()
{
if (!_isIPStyleUri)
{
string account = UriExtensions.GetAccountNameFromDomain(Host, Constants.DataLake.BlobUriSuffix);
if (account != null)
{
int serviceIndex = Host.IndexOf(Constants.DataLake.BlobUriPeriodSuffix, StringComparison.OrdinalIgnoreCase);
if (serviceIndex > 0)
{
StringBuilder stringBuilder = new StringBuilder(Host);
stringBuilder.Replace(
Constants.DataLake.BlobUriPeriodSuffix,
Constants.DataLake.DfsUriPeriodSuffix,
serviceIndex,
6);
Host = stringBuilder.ToString();
}
}
}
return ToUri();
}
/// <summary>
/// Returns the display string for the specified
/// <see cref="DataLakeUriBuilder"/> instance.
/// </summary>
/// <returns>
/// The display string for the specified <see cref="DataLakeUriBuilder"/>
/// instance.
/// </returns>
public override string ToString() =>
BuildUri().ToString();
/// <summary>
/// Reset our cached URI.
/// </summary>
private void ResetUri() =>
_uri = null;
/// <summary>
/// Construct a <see cref="RequestUriBuilder"/> representing the
/// <see cref="DataLakeUriBuilder"/>'s fields. The <see cref="Uri.Query"/>
/// property contains the SAS, snapshot, and additional query parameters.
/// </summary>
/// <returns>The constructed <see cref="RequestUriBuilder"/>.</returns>
private RequestUriBuilder BuildUri()
{
// Concatenate account, share & directory/file path (if they exist)
var path = new StringBuilder("");
// only append the account name to the path for Ip style Uri.
// regular style Uri will already have account name in domain
if (_isIPStyleUri && !string.IsNullOrWhiteSpace(AccountName))
{
path.Append('/').Append(AccountName);
}
if (!string.IsNullOrWhiteSpace(FileSystemName))
{
path.Append('/').Append(FileSystemName);
if (!string.IsNullOrWhiteSpace(DirectoryOrFilePath))
{
if (DirectoryOrFilePath == "/")
{
path.Append(_directoryOrFilePath);
}
else
{
// Encode path.
path.Append('/').Append(DirectoryOrFilePath.EscapePath());
}
}
}
// Concatenate query parameters
var query = new StringBuilder(Query);
if (!string.IsNullOrWhiteSpace(Snapshot))
{
if (query.Length > 0)
{ query.Append('&'); }
query.Append(Constants.SnapshotParameterName).Append('=').Append(Snapshot);
}
var sas = Sas?.ToString();
if (!string.IsNullOrWhiteSpace(sas))
{
if (query.Length > 0)
{ query.Append('&'); }
query.Append(sas);
}
// Use RequestUriBuilder, which has slightly nicer formatting
return new RequestUriBuilder
{
Scheme = Scheme,
Host = Host,
Port = Port,
Path = path.ToString(),
Query = query.Length > 0 ? "?" + query.ToString() : null
};
}
}
}