Innovenergy_trunk/csharp/Lib/S3Utils/S3Cfg.cs

68 lines
1.8 KiB
C#
Raw Normal View History

2023-10-02 13:45:58 +00:00
using InnovEnergy.Lib.S3Utils.DataTypes;
using static System.Environment;
namespace InnovEnergy.Lib.S3Utils;
public static class S3Cfg
{
internal static readonly String S3CfgFile = GetFolderPath(SpecialFolder.UserProfile)
.TrimEnd('\\', '/') + "/.s3cfg";
internal static Dictionary<String, String> FromFile(String cfgFilePath)
{
return File
.ReadAllLines(cfgFilePath)
.Where(l => l.Contains("="))
.Select(l => l.Split("="))
.ToDictionary(l => l[0].Trim(), l => l[1].Trim());
}
public static S3Credentials? GetDefaultUserCredentials() => GetCredentialsFromFile(S3CfgFile);
public static S3Credentials? GetCredentialsFromFile(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 = FromFile(file);
return new S3Credentials
(
Key : cfg["access_key"],
Secret: cfg["secret_key"]
);
}
catch
{
return null;
}
}
public static S3Region? GetDefaultRegionAndCredentials() => GetRegionAndCredentialsFromFile(S3CfgFile);
public static S3Region? GetRegionAndCredentialsFromFile(String file)
{
try
{
var cfg = FromFile(file);
var credentials = new S3Credentials
(
Key : cfg["access_key"],
Secret: cfg["secret_key"]
);
return new S3Region(cfg["host_base"], credentials);
2023-10-02 13:45:58 +00:00
}
catch
{
return null;
}
}
}