-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAzureSharedFSService.cs
90 lines (83 loc) · 3.48 KB
/
AzureSharedFSService.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
using Akka;
using Akka.IO;
using Akka.Streams.Dsl;
using Akka.Streams.IO;
using Azure;
using Azure.Storage.Files.Shares;
using Microsoft.Extensions.Logging;
using Snd.Sdk.Tasks;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Snd.Sdk.Storage.Base;
using Snd.Sdk.Storage.Models;
namespace Snd.Sdk.Storage.Azure
{
/// <summary>
/// Implementation of a shared filesystem for Azure Cloud.
/// </summary>
public class AzureSharedFSService : ISharedFileSystemService
{
private readonly ShareServiceClient shareServiceClient;
private readonly ILogger<AzureSharedFSService> logger;
/// <summary>
/// Create an instance of <see cref="AzureSharedFSService"/>.
/// </summary>
/// <param name="shareServiceClient">Azure Shares service client.</param>
/// <param name="logger">Logger instance for this class.</param>
public AzureSharedFSService(ShareServiceClient shareServiceClient, ILogger<AzureSharedFSService> logger)
{
this.shareServiceClient = shareServiceClient;
this.logger = logger;
}
/// <inheritdoc />
public Source<ShareFile, NotUsed> ListFiles(string fileSystemName, string path)
{
this.logger.LogDebug("Listing files from {fileSystemName} under {path}", fileSystemName, path);
var dirClient = this.shareServiceClient.GetShareClient(fileSystemName).GetDirectoryClient(path);
return Source.From(dirClient.GetFilesAndDirectories(prefix: string.Empty))
.Where(fileOrDir => !fileOrDir.IsDirectory)
.Select(file => new ShareFile
{
Name = file.Name,
Size = file.FileSize.GetValueOrDefault(0L),
ShareItemId = file.Id,
CreatedOn = file.Properties?.CreatedOn,
LastModifiedOn = file.Properties?.LastModified
});
}
/// <inheritdoc />
public Source<ByteString, Task<IOResult>> ReadTextFile(string fileSystemName, string path, string fileName, int bufferSize = 4194304)
{
var dirClient = this.shareServiceClient.GetShareClient(fileSystemName).GetDirectoryClient(path);
var fileClient = dirClient.GetFileClient(fileName);
return StreamConverters.FromInputStream(() =>
{
try
{
return fileClient.OpenRead(allowfileModifications: true, bufferSize: bufferSize);
}
catch (RequestFailedException ex)
{
this.logger.LogWarning(ex, "Failed to read logs for {fileName}", fileName);
return new MemoryStream(Encoding.UTF8.GetBytes($"Not found: {fileName}\n"));
}
}, chunkSize: (int)(bufferSize * 0.9));
}
/// <inheritdoc />
public Task<bool> RemoveFile(string fileSystemName, string path, string fileName)
{
var dirClient = this.shareServiceClient.GetShareClient(fileSystemName).GetDirectoryClient(path);
var fileClient = dirClient.GetFileClient(fileName);
try
{
return fileClient.DeleteAsync().Map(_ => true);
}
catch (RequestFailedException ex)
{
this.logger.LogError(ex, "Failed to delete {path}/{fileName}", path, fileName);
return Task.FromResult(false);
}
}
}
}