Innovenergy_trunk/csharp/App/SaliMax/src/S3Config.cs

85 lines
3.0 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Security.Cryptography;
using Flurl;
using Flurl.Http;
using InnovEnergy.Lib.Utils;
using static System.Text.Encoding;
using Convert = System.Convert;
namespace InnovEnergy.App.SaliMax;
2023-02-16 12:57:06 +00:00
public record S3Config
{
public required String Bucket { get; init; }
public required String Region { get; init; }
public required String Provider { get; init; }
public required String Key { get; init; }
public required String Secret { get; init; }
public required String ContentType { get; init; }
2023-02-16 12:57:06 +00:00
public String Host => $"{Bucket}.{Region}.{Provider}";
public String Url => $"https://{Host}";
public IFlurlRequest CreatePutRequest(String s3Path) => CreateRequest("PUT", s3Path);
public IFlurlRequest CreateGetRequest(String s3Path) => CreateRequest("GET", s3Path);
private IFlurlRequest CreateRequest(String method, String s3Path)
{
var date = DateTime.UtcNow.ToString("r");
var auth = CreateAuthorization(method, s3Path, date);
return Url
.AppendPathSegment(s3Path)
.WithHeader("Host", Host)
.WithHeader("Date", date)
.WithHeader("Authorization", auth)
.AllowAnyHttpStatus();
}
private String CreateAuthorization(String method,
String s3Path,
String date)
{
return CreateAuthorization
(
method : method,
bucket : Bucket,
s3Path : s3Path,
date : date,
s3Key : Key,
s3Secret : Secret,
contentType: ContentType
);
}
private static String CreateAuthorization(String method,
String bucket,
String s3Path,
String date,
String s3Key,
String s3Secret,
String contentType = "application/base64",
2023-02-16 12:57:06 +00:00
String md5Hash = "")
{
// StringToSign = HTTP-Verb + "\n" +
// Content-MD5 + "\n" +
// Content-Type + "\n" +
// Date + "\n" +
// CanonicalizedAmzHeaders +
// CanonicalizedResource;
contentType = "application/base64; charset=utf-8";
2023-02-22 13:46:36 +00:00
var payload = $"{method}\n{md5Hash}\n{contentType}\n{date}\n/{bucket.Trim('/')}/{s3Path.Trim('/')}";
2023-02-16 12:57:06 +00:00
using var hmacSha1 = new HMACSHA1(UTF8.GetBytes(s3Secret));
2023-02-22 13:46:36 +00:00
2023-02-16 12:57:06 +00:00
var signature = UTF8
.GetBytes(payload)
.Apply(hmacSha1.ComputeHash)
.Apply(Convert.ToBase64String);
return $"AWS {s3Key}:{signature}";
}
}