-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Sample01b_HelloWorldAsync.cs
702 lines (597 loc) · 32.2 KB
/
Sample01b_HelloWorldAsync.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using NUnit.Framework;
namespace Azure.Storage.Files.DataLake.Samples
{
/// <summary>
/// Basic Azure DataLake Storage samples.
/// </summary>
public class Sample01b_HelloWorldAsync : SampleTest
{
/// <summary>
/// Create a DataLake File using a DataLakeSystem
/// </summary>
[Test]
public async Task CreateFileClientAsync_Filesystem()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-append" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-append"));
filesystem.Create();
// Create
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
await file.CreateAsync();
// Verify we created one file
List<PathItem> paths = new List<PathItem>();
await foreach (PathItem path in filesystem.GetPathsAsync())
{
paths.Add(path);
}
Assert.AreEqual(1, paths.Count);
// Cleanup
await filesystem.DeleteAsync();
}
/// <summary>
/// Create a DataLake File using a DataLake Directory.
/// </summary>
[Test]
public async Task CreateFileClientAsync_Directory()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Create a DataLake Filesystem
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem"));
await filesystem.CreateAsync();
//Create a DataLake Directory
DataLakeDirectoryClient directory = filesystem.CreateDirectory(Randomize("sample-directory"));
await directory.CreateAsync();
// Create a DataLake File using a DataLake Directory
DataLakeFileClient file = directory.GetFileClient(Randomize("sample-file"));
await file.CreateAsync();
// Verify we created one file
List<PathItem> paths = new List<PathItem>();
await foreach (PathItem path in filesystem.GetPathsAsync())
{
paths.Add(path);
}
Assert.AreEqual(1, paths.Count);
// Cleanup
await filesystem.DeleteAsync();
}
/// <summary>
/// Create a DataLake Directory.
/// </summary>
[Test]
public async Task CreateDirectoryClientAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-append" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-append"));
filesystem.Create();
// Create
DataLakeDirectoryClient directory = filesystem.GetDirectoryClient(Randomize("sample-file"));
await directory.CreateAsync();
// Verify we created one directory
List<PathItem> paths = new List<PathItem>();
await foreach (PathItem path in filesystem.GetPathsAsync())
{
paths.Add(path);
}
Assert.AreEqual(1, paths.Count);
// Cleanup
await filesystem.DeleteAsync();
}
/// <summary>
/// Upload a file to a DataLake File.
/// </summary>
[Test]
public async Task AppendAsync_Simple()
{
// Create Sample File to read content from
string sampleFilePath = CreateTempFile(SampleFileContent);
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-append" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-append"));
await filesystem.CreateAsync();
try
{
// Create a file
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
await file.CreateAsync();
// Append data to the DataLake File
await file.AppendAsync(File.OpenRead(sampleFilePath), 0);
await file.FlushAsync(SampleFileContent.Length);
// Verify the contents of the file
PathProperties properties = await file.GetPropertiesAsync();
Assert.AreEqual(SampleFileContent.Length, properties.ContentLength);
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Upload file by created a file, and then appending each part to a DataLake File.
/// </summary>
[Test]
public async Task AppendAsync()
{
// Create three temporary Lorem Ipsum files on disk that we can upload
int contentLength = 10;
string sampleFileContentPart1 = CreateTempFile(SampleFileContent.Substring(0, contentLength));
string sampleFileContentPart2 = CreateTempFile(SampleFileContent.Substring(contentLength, contentLength));
string sampleFileContentPart3 = CreateTempFile(SampleFileContent.Substring(contentLength * 2, contentLength));
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Get a reference to a FileSystemClient
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-appendasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-append"));
await filesystem.CreateAsync();
try
{
// Get a reference to a file named "sample-file" in a filesystem
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
// Create the file
await file.CreateAsync();
// Verify we created one file
List<PathItem> paths = new List<PathItem>();
await foreach (PathItem path in filesystem.GetPathsAsync())
{
paths.Add(path);
}
Assert.AreEqual(1, paths.Count);
// Append data to an existing DataLake File. Append is currently limited to 4000 MB per call.
// To upload a large file all at once, consider using UploadAsync() instead.
await file.AppendAsync(File.OpenRead(sampleFileContentPart1), 0);
await file.AppendAsync(File.OpenRead(sampleFileContentPart2), contentLength);
await file.AppendAsync(File.OpenRead(sampleFileContentPart3), contentLength * 2);
await file.FlushAsync(contentLength * 3);
// Verify the contents of the file
PathProperties properties = await file.GetPropertiesAsync();
Assert.AreEqual(contentLength * 3, properties.ContentLength);
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Upload file by calling the UploadAsync API.
/// </summary>
[Test]
public async Task UploadAsync()
{
// Create three temporary Lorem Ipsum files on disk that we can upload
int contentLength = 10;
string sampleFileContent = CreateTempFile(SampleFileContent.Substring(0, contentLength));
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Get a reference to a FileSystemClient
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-appendasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-append"));
await filesystem.CreateAsync();
try
{
// Get a reference to a file named "sample-file" in a filesystem
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
// Upload content to the file. When using the Upload API, you don't need to create the file first.
// If the file already exists, it will be overwritten.
// For larger files, Upload() will upload the file in multiple parallel requests.
await file.UploadAsync(File.OpenRead(sampleFileContent));
// Verify the contents of the file
PathProperties properties = await file.GetPropertiesAsync();
Assert.AreEqual(contentLength, properties.ContentLength);
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Download a DataLake File to a file.
/// </summary>
[Test]
public async Task ReadAsync()
{
// Create a temporary Lorem Ipsum file on disk that we can upload
string originalPath = CreateTempFile(SampleFileContent);
// Get a temporary path on disk where we can download the file
string downloadPath = CreateTempPath();
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-readasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-read"));
await filesystem.CreateAsync();
try
{
// Get a reference to a file named "sample-file" in a filesystem
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
// First upload something the DataLake file so we have something to download
await file.UploadAsync(File.OpenRead(originalPath));
// Download the DataLake file's contents and save it to a file
// The ReadAsync() API downloads a file in a single requests.
// For large files, it may be faster to call ReadToAsync()
Response<FileDownloadInfo> fileContents = await file.ReadAsync();
using (FileStream stream = File.OpenWrite(downloadPath))
{
fileContents.Value.Content.CopyTo(stream);
}
// Verify the contents
Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath));
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Download a DataLake File directly to file.
/// </summary>
[Test]
public async Task ReadToAsync()
{
// Create a temporary Lorem Ipsum file on disk that we can upload
string originalPath = CreateTempFile(SampleFileContent);
// Get a temporary path on disk where we can download the file
string downloadPath = CreateTempPath();
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-readasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-read"));
await filesystem.CreateAsync();
try
{
// Get a reference to a file named "sample-file" in a filesystem
DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));
// First upload something the DataLake file so we have something to download
await file.UploadAsync(File.OpenRead(originalPath));
// Download the DataLake file's contents directly to a file.
// For larger files, ReadToAsync() will download the file in multiple parallel requests.
await file.ReadToAsync(downloadPath);
// Verify the contents
Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath));
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// List all the DataLake directories in a filesystem.
/// </summary>
[Test]
public async Task ListAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-listasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-list"));
await filesystem.CreateAsync();
try
{
// Upload a couple of directories so we have something to list
await filesystem.CreateDirectoryAsync("sample-directory1");
await filesystem.CreateDirectoryAsync("sample-directory2");
await filesystem.CreateDirectoryAsync("sample-directory3");
// List all the directories
List<string> names = new List<string>();
await foreach (PathItem pathItem in filesystem.GetPathsAsync())
{
names.Add(pathItem.Name);
}
Assert.AreEqual(3, names.Count);
Assert.Contains("sample-directory1", names);
Assert.Contains("sample-directory2", names);
Assert.Contains("sample-directory3", names);
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Traverse the DataLake Files and DataLake Directories in a DataLake filesystem.
/// </summary>
[Test]
public async Task TraverseAsync()
{
// Create a temporary Lorem Ipsum file on disk that we can upload
string originalPath = CreateTempFile(SampleFileContent);
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-traverseasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-traverse"));
await filesystem.CreateAsync();
try
{
// Create a bunch of directories and files within the directories
DataLakeDirectoryClient first = await filesystem.CreateDirectoryAsync("first");
await first.CreateSubDirectoryAsync("a");
await first.CreateSubDirectoryAsync("b");
DataLakeDirectoryClient second = await filesystem.CreateDirectoryAsync("second");
await second.CreateSubDirectoryAsync("c");
await second.CreateSubDirectoryAsync("d");
await filesystem.CreateDirectoryAsync("third");
DataLakeDirectoryClient fourth = await filesystem.CreateDirectoryAsync("fourth");
DataLakeDirectoryClient deepest = await fourth.CreateSubDirectoryAsync("e");
// Upload a DataLake file named "file"
DataLakeFileClient file = deepest.GetFileClient("file");
await file.CreateAsync();
using (FileStream stream = File.OpenRead(originalPath))
{
await file.AppendAsync(stream, 0);
}
// Keep track of all the names we encounter
List<string> names = new List<string>();
await foreach (PathItem pathItem in filesystem.GetPathsAsync(recursive: true))
{
names.Add(pathItem.Name);
}
// Verify we've seen everything
Assert.AreEqual(10, names.Count);
Assert.Contains("first", names);
Assert.Contains("second", names);
Assert.Contains("third", names);
Assert.Contains("fourth", names);
Assert.Contains("first/a", names);
Assert.Contains("first/b", names);
Assert.Contains("second/c", names);
Assert.Contains("second/d", names);
Assert.Contains("fourth/e", names);
Assert.Contains("fourth/e/file", names);
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Trigger a recoverable error.
/// </summary>
[Test]
public async Task ErrorsAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a container named "sample-filesystem-errorsasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-errors"));
await filesystem.CreateAsync();
try
{
// Try to create the filesystem again
await filesystem.CreateAsync();
}
catch (RequestFailedException ex)
when (ex.ErrorCode == "ContainerAlreadyExists")
{
// Ignore any errors if the filesystem already exists
}
catch (RequestFailedException ex)
{
Assert.Fail($"Unexpected error: {ex}");
}
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
/// <summary>
/// Set permissions in the access control list and gets access control list on a DataLake File
/// </summary>
[Test]
public async Task SetPermissionsAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = NamespaceStorageAccountName;
string storageAccountKey = NamespaceStorageAccountKey;
Uri serviceUri = NamespaceBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-aclasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-perasync"));
await filesystem.CreateAsync();
try
{
// Create a DataLake file so we can set the Access Controls on the files
DataLakeFileClient fileClient = filesystem.GetFileClient(Randomize("sample-file"));
await fileClient.CreateAsync();
// Set the Permissions of the file
PathPermissions pathPermissions = PathPermissions.ParseSymbolicPermissions("rwxrwxrwx");
await fileClient.SetPermissionsAsync(permissions: pathPermissions);
// Get Access Control List
PathAccessControl accessControlResponse = await fileClient.GetAccessControlAsync();
// Check Access Control permissions
Assert.AreEqual(pathPermissions.ToSymbolicPermissions(), accessControlResponse.Permissions.ToSymbolicPermissions());
Assert.AreEqual(pathPermissions.ToOctalPermissions(), accessControlResponse.Permissions.ToOctalPermissions());
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Set and gets access control list on a DataLake File.
/// </summary>
[Test]
public async Task SetGetAclsAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = NamespaceStorageAccountName;
string storageAccountKey = NamespaceStorageAccountKey;
Uri serviceUri = NamespaceBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-aclasync" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-acl"));
await filesystem.CreateAsync();
try
{
// Create a DataLake file so we can set the Access Controls on the files
DataLakeFileClient fileClient = filesystem.GetFileClient(Randomize("sample-file"));
await fileClient.CreateAsync();
IList<PathAccessControlItem> accessControlList = PathAccessControlExtensions.ParseAccessControlList("user::rwx,group::r--,mask::rwx,other::---");
// Set Access Control List
await fileClient.SetAccessControlListAsync(accessControlList);
// Get Access Control List
PathAccessControl accessControlResponse = await fileClient.GetAccessControlAsync();
// Check Access Control permissions
Assert.AreEqual(
PathAccessControlExtensions.ToAccessControlListString(accessControlList),
PathAccessControlExtensions.ToAccessControlListString(accessControlResponse.AccessControlList.ToList()));
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Rename a DataLake file and a DatLake directories in a DataLake Filesystem.
/// </summary>
[Test]
public async Task RenameAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-rename" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-rename"));
await filesystem.CreateAsync();
try
{
// Create a DataLake Directory to rename it later
DataLakeDirectoryClient directoryClient = filesystem.GetDirectoryClient(Randomize("sample-directory"));
await directoryClient.CreateAsync();
// Rename directory with new path/name and verify by making a service call (e.g. GetProperties)
DataLakeDirectoryClient renamedDirectoryClient = await directoryClient.RenameAsync("sample-directory2");
PathProperties directoryPathProperties = await renamedDirectoryClient.GetPropertiesAsync();
// Delete the sample directory using the new path/name
await filesystem.DeleteDirectoryAsync("sample-directory2");
// Create a DataLake file.
DataLakeFileClient fileClient = filesystem.GetFileClient(Randomize("sample-file"));
await fileClient.CreateAsync();
// Rename file with new path/name and verify by making a service call (e.g. GetProperties)
DataLakeFileClient renamedFileClient = await fileClient.RenameAsync("sample-file2");
PathProperties pathProperties = await renamedFileClient.GetPropertiesAsync();
// Delete the sample directory using the new path/name
await filesystem.DeleteFileAsync("sample-file2");
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
/// <summary>
/// Get Properties on a DataLake File and a Directory
/// </summary>
[Test]
public async Task GetPropertiesAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = StorageAccountName;
string storageAccountKey = StorageAccountKey;
Uri serviceUri = StorageAccountBlobUri;
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);
// Get a reference to a filesystem named "sample-filesystem-rename" and then create it
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem"));
await filesystem.CreateAsync();
try
{
// Create a DataLake Directory to rename it later
DataLakeDirectoryClient directoryClient = filesystem.GetDirectoryClient(Randomize("sample-directory"));
await directoryClient.CreateAsync();
// Get Properties on a Directory
PathProperties directoryPathProperties = await directoryClient.GetPropertiesAsync();
// Create a DataLake file
DataLakeFileClient fileClient = filesystem.GetFileClient(Randomize("sample-file"));
await fileClient.CreateAsync();
// Get Properties on a File
PathProperties filePathProperties = await fileClient.GetPropertiesAsync();
}
finally
{
// Clean up after the test when we're finished
await filesystem.DeleteAsync();
}
}
}
}