2023-03-19 15:09:37 +00:00
|
|
|
using CliWrap;
|
|
|
|
using CliWrap.Buffered;
|
|
|
|
using InnovEnergy.Lib.Utils;
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.S3;
|
|
|
|
|
|
|
|
public class S3Cmd
|
|
|
|
{
|
|
|
|
private static readonly Command Python = Cli.Wrap("python3");
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
private String[] DefaultArgs { get; }
|
|
|
|
|
|
|
|
// ReSharper disable StringLiteralTypo
|
|
|
|
// ReSharper enable StringLiteralTypo
|
|
|
|
|
|
|
|
public S3Cmd(String key, String secret)
|
|
|
|
{
|
|
|
|
DefaultArgs = new[]
|
|
|
|
{
|
|
|
|
S3CmdPath,
|
|
|
|
"--access_key", key,
|
|
|
|
"--secret_key", secret,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var result = await Run(bucketName, "mb");
|
|
|
|
return result.ExitCode == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var args = DefaultArgs
|
|
|
|
.Append(operation)
|
2023-03-19 16:02:18 +00:00
|
|
|
.Append(bucketName.EnsureStartsWith(S3Prefix))
|
2023-03-19 15:09:37 +00:00
|
|
|
.Concat(optionalArgs);
|
|
|
|
|
|
|
|
return Python
|
|
|
|
.WithArguments(args)
|
|
|
|
.ExecuteBufferedAsync();
|
|
|
|
}
|
|
|
|
}
|