-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3fileprovider.cs
97 lines (78 loc) · 3.23 KB
/
s3fileprovider.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
// Licensed under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
using System.Linq;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.FileProviders;
using Amazon.S3;
namespace Evorine.FileSystem.S3FileProvider
{
/// <summary>
/// Looks up files on AWS S3 file system.
/// </summary>
/// <remarks>
/// It is readonly.
/// </remarks>
public class S3FileProvider : IFileProvider, IDisposable
{
private static readonly char[] pathSeparators = new[] { '/' };
private static readonly char[] invalidFileNameChars = new[] { '\\', '{', '}', '^', '%', '`', '[', ']', '\'', '"', '>', '<', '~', '#', '|' }
.Concat(Enumerable.Range(128, 255).Select(x => (char)x))
.ToArray();
readonly IAmazonS3 amazonS3;
readonly string bucketName;
/// <summary>
/// Initializes a new instance of a <see cref="S3FileProvider"/> at the given bucket.
/// </summary>
/// <param name="amazonS3"><see cref="IAmazonS3" /> Amazon S3 service object</param>
/// <param name="bucketName">Name of the bucket that will be used</param>
public S3FileProvider(IAmazonS3 amazonS3, string bucketName)
{
this.amazonS3 = amazonS3;
this.bucketName = bucketName;
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
if (subpath == null) throw new ArgumentNullException(nameof(subpath));
if (HasInvalidFileNameChars(subpath)) return NotFoundDirectoryContents.Singleton;
// Relative paths starting with leading slashes are okay
subpath = subpath.TrimStart(pathSeparators);
return new S3DirectoryContents(amazonS3, bucketName, subpath);
}
/// <summary>
/// Locates a file at the given path.
/// </summary>
/// <param name="subpath">A path under the bucket</param>
/// <returns>The file information. Caller must check Exists property.</returns>
public IFileInfo GetFileInfo(string subpath)
{
if (subpath == null) throw new ArgumentNullException(nameof(subpath));
if (HasInvalidFileNameChars(subpath)) return new NotFoundFileInfo(subpath);
// Relative paths starting with leading slashes are okay
subpath = subpath.TrimStart(pathSeparators);
if (string.IsNullOrEmpty(subpath))
return new NotFoundFileInfo(subpath);
return new S3FileInfo(amazonS3, bucketName, subpath);
}
/// <summary>
/// Watch is not supported.
/// </summary>
public IChangeToken Watch(string filter)
{
return NullChangeToken.Singleton;
}
/// <summary>
/// Disposes the file provider.
/// </summary>
public void Dispose()
{
amazonS3.Dispose();
}
private bool HasInvalidFileNameChars(string path)
{
return path.IndexOfAny(invalidFileNameChars) != -1;
}
}
}