-
Notifications
You must be signed in to change notification settings - Fork 0
/
adls.cs
75 lines (58 loc) · 3.22 KB
/
adls.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
using Azure;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using Azure.Identity;
// dotnet add package Azure.Storage.Files.DataLake
// dotnet add package Azure.Identity
namespace adls;
class Program
{
static async Task Main(string[] args)
{
// Create a dataLakeServiceClient
var credential = new ClientSecretCredential(AzureVariables.tenantId, AzureVariables.clientId, AzureVariables.clientSecret);
Uri dataLakeEndpoint = new Uri(AzureVariables.adlsEndpoint);
DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(dataLakeEndpoint, credential);
// Create a dataLakeFileSystemClient
string containerName = "iowa-json";
DataLakeFileSystemClient dataLakeFileSystemClient = dataLakeServiceClient.GetFileSystemClient(containerName);
// Create a new folder
DataLakeDirectoryClient dataLakeDirectoryClient00 = await dataLakeFileSystemClient.CreateDirectoryAsync("adls_json_upload");
System.Console.WriteLine($"Directory: {dataLakeDirectoryClient00.Name} created");
// Rename a folder
DataLakeDirectoryClient dataLakeDirectoryClient01 = dataLakeFileSystemClient.GetDirectoryClient("adls_json_upload");
DataLakeDirectoryClient dataLakeDirectoryClient02 = await dataLakeDirectoryClient01.RenameAsync("adls-json_upload-01");
System.Console.WriteLine($"Directory {dataLakeDirectoryClient01.Name} has been renamed. New name: {dataLakeDirectoryClient02.Name}");
// // Delete the directory
// await dataLakeDirectoryClient02.DeleteAsync();
// Upload a file
string fileName = "09.json";
string fileToUpload = Path.Combine("json_files", fileName);
DataLakeFileClient fileToUploadClient = await dataLakeDirectoryClient02.CreateFileAsync(fileName);
FileStream fileStream00 = File.OpenRead(fileToUpload);
await fileToUploadClient.AppendAsync(fileStream00, offset: 0);
await fileToUploadClient.FlushAsync(position: fileStream00.Length);
// List files in a directory
await foreach (PathItem pathItem in dataLakeDirectoryClient02.GetPathsAsync(recursive: true))
{
System.Console.WriteLine($"File: {pathItem.Name}");
}
// Download/read a file
DataLakeFileClient fileToDownload = dataLakeDirectoryClient02.GetFileClient("09.json");
Response<FileDownloadInfo> downloadResponse = await fileToDownload.ReadAsync();
BinaryReader reader = new BinaryReader(downloadResponse.Value.Content);
FileStream fileStream01 = File.OpenWrite("json_files/09-01.json");
byte[] buffer = new byte[4096];
int count;
while((count = reader.Read(buffer, 0, buffer.Length)) != 0)
{
fileStream01.Write(buffer, 0, count);
}
await fileStream01.FlushAsync();
fileStream01.Close();
// Rename a file and move to another folder - the new folder must exist
await fileToDownload.RenameAsync("adls-json_upload-02/09-03.json");
// Delete a file
// await dataLakeFileSystemClient.DeleteFileAsync("adls-json_upload-01/09.json");
}
}