47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
|
using static System.Environment;
|
||
|
|
||
|
namespace InnovEnergy.Lib.S3Utils.Data;
|
||
|
|
||
|
public record S3Credentials
|
||
|
{
|
||
|
protected static readonly String S3CfgFile = GetFolderPath(SpecialFolder.UserProfile).TrimEnd('\\', '/') + "/.s3cfg";
|
||
|
|
||
|
public required String Key { get; init; }
|
||
|
public required String Secret { get; init; }
|
||
|
|
||
|
public static S3Credentials? FromS3Cfg() => FromFile(S3CfgFile);
|
||
|
|
||
|
public static S3Credentials? FromFile(String file)
|
||
|
{
|
||
|
// [default]
|
||
|
// host_base = sos-ch-dk-2.exo.io
|
||
|
// host_bucket = %(bucket)s.sos-ch-dk-2.exo.io
|
||
|
// access_key = xxxxxxxxxxxxxxx
|
||
|
// secret_key = xxxxxxxxxxxxxxx
|
||
|
// use_https = True
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var cfg = ParseFile(file);
|
||
|
|
||
|
return new S3Credentials
|
||
|
{
|
||
|
Key = cfg["access_key"],
|
||
|
Secret = cfg["secret_key"]
|
||
|
};
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected static Dictionary<String, String> ParseFile(String cfgFile)
|
||
|
{
|
||
|
return File
|
||
|
.ReadAllLines(cfgFile)
|
||
|
.Where(l => l.Contains("="))
|
||
|
.Select(l => l.Split("="))
|
||
|
.ToDictionary(l => l[0].Trim(), l => l[1].Trim());
|
||
|
}
|
||
|
}
|