2023-03-23 12:28:55 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-03-19 15:09:37 +00:00
|
|
|
using CliWrap;
|
|
|
|
using CliWrap.Buffered;
|
|
|
|
using InnovEnergy.Lib.Utils;
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.S3;
|
|
|
|
|
2023-03-23 12:28:55 +00:00
|
|
|
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
|
2023-03-19 15:09:37 +00:00
|
|
|
public class S3Cmd
|
|
|
|
{
|
|
|
|
private static readonly Command Python = Cli.Wrap("python3");
|
|
|
|
|
2023-03-23 11:47:25 +00:00
|
|
|
private const String? S3CmdPath = "Resources/s3cmd.py";
|
2023-03-19 16:02:18 +00:00
|
|
|
private const String S3Prefix = "s3://";
|
2023-03-19 15:09:37 +00:00
|
|
|
|
2023-03-23 12:28:55 +00:00
|
|
|
public String Key { get; init;}
|
|
|
|
public String Secret { get; init;}
|
|
|
|
|
|
|
|
// private String?[] DefaultArgs { get; }
|
2023-03-19 15:09:37 +00:00
|
|
|
|
|
|
|
// ReSharper disable StringLiteralTypo
|
|
|
|
// ReSharper enable StringLiteralTypo
|
2023-03-23 12:28:55 +00:00
|
|
|
[Obsolete("Only to be used by Json-Deserializer")] public S3Cmd()
|
2023-03-19 15:09:37 +00:00
|
|
|
{
|
|
|
|
}
|
2023-03-23 12:28:55 +00:00
|
|
|
|
|
|
|
// public S3Cmd(String? key, String? secret)
|
|
|
|
// {
|
|
|
|
// DefaultArgs = new[]
|
|
|
|
// {
|
|
|
|
// S3CmdPath,
|
|
|
|
// "--access_key", key,
|
|
|
|
// "--secret_key", secret,
|
|
|
|
// };
|
|
|
|
// }
|
2023-03-19 15:09:37 +00:00
|
|
|
|
|
|
|
public async Task<String> SignUrl(String bucketName, TimeSpan validity)
|
|
|
|
{
|
|
|
|
var result = await Run(bucketName, "signurl", $"+{validity.TotalSeconds}");
|
|
|
|
|
|
|
|
return result
|
|
|
|
.StandardOutput
|
|
|
|
.Replace("\n", "")
|
|
|
|
.Replace(" ", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Boolean> CreateBucket(String bucketName)
|
|
|
|
{
|
2023-07-27 11:23:17 +00:00
|
|
|
const String cors = "./Resources/CORS";
|
2023-07-13 08:16:31 +00:00
|
|
|
|
2023-03-19 15:09:37 +00:00
|
|
|
var result = await Run(bucketName, "mb");
|
2023-07-13 14:17:58 +00:00
|
|
|
var setCors = await Run(bucketName, "setcors", cors);
|
2023-07-13 08:01:30 +00:00
|
|
|
|
|
|
|
return result.ExitCode == 0 && setCors.ExitCode == 0;
|
2023-03-19 15:09:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 14:17:58 +00:00
|
|
|
public async Task<String> ListFilesInBucket(String bucketName)
|
|
|
|
{
|
|
|
|
var result = await Run(bucketName, "ls");
|
|
|
|
return result.StandardOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<String[]> GetFileText(String bucketName, String filename)
|
|
|
|
{
|
|
|
|
var result = await Run(bucketName + "/" + filename, "get", "--force");
|
|
|
|
return File.ReadAllLines("./" + filename);
|
|
|
|
}
|
|
|
|
|
2023-03-19 15:09:37 +00:00
|
|
|
public async Task<Boolean> DeleteBucket(String bucketName)
|
|
|
|
{
|
|
|
|
var result = await Run(bucketName, "rb");
|
|
|
|
return result.ExitCode == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task<BufferedCommandResult> Run(String bucketName, String operation, params String[] optionalArgs)
|
|
|
|
{
|
2023-03-23 12:28:55 +00:00
|
|
|
var credentials = new String[]
|
|
|
|
{
|
|
|
|
S3CmdPath,
|
|
|
|
"--access_key", Key,
|
|
|
|
"--secret_key", Secret,
|
|
|
|
};
|
|
|
|
|
|
|
|
var args = credentials
|
2023-03-19 15:09:37 +00:00
|
|
|
.Append(operation)
|
2023-07-27 11:23:17 +00:00
|
|
|
.Concat(optionalArgs)
|
|
|
|
.Append(bucketName.EnsureStartsWith(S3Prefix));
|
2023-03-19 15:09:37 +00:00
|
|
|
|
|
|
|
return Python
|
2023-03-23 12:28:55 +00:00
|
|
|
.WithArguments(args)
|
|
|
|
.ExecuteBufferedAsync();
|
2023-03-19 15:09:37 +00:00
|
|
|
}
|
2023-07-13 14:17:58 +00:00
|
|
|
|
2023-03-19 15:09:37 +00:00
|
|
|
}
|