-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAmazonS3BlobPath.cs
73 lines (61 loc) · 2.43 KB
/
AmazonS3BlobPath.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
using System;
using System.Text.RegularExpressions;
using Snd.Sdk.Storage.Models.Base;
namespace Snd.Sdk.Storage.Models.BlobPath;
/// <summary>
/// Amazon S3 path.
/// </summary>
public record AmazonS3StoragePath : IStoragePath
{
private const string matchRegex = "s3a://(?<bucket>[^/]+)/?(?<key>.*)";
/// <summary>
/// Blob bucket name
/// </summary>
public string Bucket { get; init; }
/// <inheritdoc cref="IStoragePath.ObjectKey"/>
public string ObjectKey { get; init; }
/// <inheritdoc cref="IStoragePath.ToHdfsPath"/>
public string ToHdfsPath() => $"s3a://{this.Bucket}/{this.ObjectKey}";
/// <inheritdoc cref="IStoragePath"/>
public AmazonS3StoragePath Join(string keyName)
{
return this with
{
ObjectKey = string.IsNullOrEmpty(this.ObjectKey) ? keyName : $"{this.ObjectKey}/{keyName}"
};
}
/// <summary>
/// Converts HDFS path to an instance of <see cref="AmazonS3StoragePath"/>.
/// </summary>
/// <param name="hdfspath">HDFS path in format s3a://bucket/path</param>
/// <returns>Path instance</returns>
/// <exception cref="ArgumentException">If path does not match the format</exception>
public AmazonS3StoragePath(string hdfspath)
{
var regex = new Regex(matchRegex);
var match = regex.Match(hdfspath);
if (!match.Success)
{
throw new ArgumentException($"An {nameof(AmazonS3StoragePath)} must be in the format s3a://bucket/path, but was: {hdfspath}");
}
this.Bucket = match.Groups["bucket"].Value;
this.ObjectKey = match.Groups["key"].Value.Trim('/');
}
/// <summary>
/// Creates a new instance of <see cref="AmazonS3StoragePath"/> with the specified bucket and key.
/// </summary>
/// <param name="bucket">Bucket name</param>
/// <param name="key">Key within the bucket</param>
/// <exception cref="ArgumentException"></exception>
public AmazonS3StoragePath(string bucket, string key)
{
this.Bucket = bucket.Trim('/');
this.ObjectKey = key.Trim('/');
}
/// <summary>
/// Tests is path can be converted to <see cref="AmazonS3StoragePath"/>
/// </summary>
/// <param name="hdfsPath">Path to check</param>
/// <returns>True if path con be converted to <see cref="AmazonS3StoragePath"/></returns>
public static bool IsAmazonS3Path(string hdfsPath) => new Regex(matchRegex).IsMatch(hdfsPath);
}