forked from mtusi2026/mtusi2026lab4
103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// S3Client uses AWS Signature V4 with path-style requests (MinIO-compatible).
|
|
// No credentials or bucket identifiers are returned through the public API.
|
|
type S3Client struct {
|
|
Endpoint, Region, Bucket, Key, Secret string
|
|
HTTP *http.Client
|
|
}
|
|
|
|
func NewS3Client() (*S3Client, error) {
|
|
s := &S3Client{Endpoint: strings.TrimRight(os.Getenv("S3_ENDPOINT"), "/"), Region: os.Getenv("S3_REGION"), Bucket: os.Getenv("S3_BUCKET"), Key: os.Getenv("S3_ACCESS_KEY_ID"), Secret: os.Getenv("S3_SECRET_ACCESS_KEY"), HTTP: &http.Client{Timeout: 12 * time.Second}}
|
|
if s.Region == "" {
|
|
s.Region = "us-east-1"
|
|
}
|
|
if s.Endpoint == "" || s.Bucket == "" || s.Key == "" || s.Secret == "" {
|
|
return nil, errors.New("missing S3 configuration")
|
|
}
|
|
u, err := url.Parse(s.Endpoint)
|
|
if err != nil || u.Scheme != "http" && u.Scheme != "https" || u.Host == "" {
|
|
return nil, errors.New("invalid S3 endpoint")
|
|
}
|
|
return s, nil
|
|
}
|
|
func mac(key []byte, value string) []byte {
|
|
m := hmac.New(sha256.New, key)
|
|
m.Write([]byte(value))
|
|
return m.Sum(nil)
|
|
}
|
|
func hexsum(data []byte) string { v := sha256.Sum256(data); return hex.EncodeToString(v[:]) }
|
|
func (s *S3Client) request(ctx context.Context, method, key, contentType string, body []byte) ([]byte, error) {
|
|
path := "/" + s.Bucket
|
|
if key != "" {
|
|
path += "/" + strings.ReplaceAll(url.PathEscape(key), "%2F", "/")
|
|
}
|
|
endpoint, err := url.Parse(s.Endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target := s.Endpoint + path
|
|
req, err := http.NewRequestWithContext(ctx, method, target, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
now := time.Now().UTC()
|
|
date := now.Format("20060102")
|
|
amzDate := now.Format("20060102T150405Z")
|
|
digest := hexsum(body)
|
|
req.Header.Set("x-amz-date", amzDate)
|
|
req.Header.Set("x-amz-content-sha256", digest)
|
|
if contentType != "" {
|
|
req.Header.Set("Content-Type", contentType)
|
|
}
|
|
canonicalHeaders := "host:" + endpoint.Host + "\n" + "x-amz-content-sha256:" + digest + "\n" + "x-amz-date:" + amzDate + "\n"
|
|
signedHeaders := "host;x-amz-content-sha256;x-amz-date"
|
|
canonical := method + "\n" + path + "\n\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + digest
|
|
scope := date + "/" + s.Region + "/s3/aws4_request"
|
|
toSign := "AWS4-HMAC-SHA256\n" + amzDate + "\n" + scope + "\n" + hexsum([]byte(canonical))
|
|
signingKey := mac(mac(mac(mac([]byte("AWS4"+s.Secret), date), s.Region), "s3"), "aws4_request")
|
|
auth := "AWS4-HMAC-SHA256 Credential=" + s.Key + "/" + scope + ", SignedHeaders=" + signedHeaders + ", Signature=" + hex.EncodeToString(mac(signingKey, toSign))
|
|
req.Header.Set("Authorization", auth)
|
|
resp, err := s.HTTP.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
io.Copy(io.Discard, io.LimitReader(resp.Body, 4096))
|
|
return nil, fmt.Errorf("S3 returned HTTP %d", resp.StatusCode)
|
|
}
|
|
return io.ReadAll(io.LimitReader(resp.Body, 10*1024*1024+1))
|
|
}
|
|
func (s *S3Client) Put(ctx context.Context, key, mime string, body []byte) error {
|
|
_, err := s.request(ctx, "PUT", key, mime, body)
|
|
return err
|
|
}
|
|
func (s *S3Client) Get(ctx context.Context, key string) ([]byte, error) {
|
|
return s.request(ctx, "GET", key, "", nil)
|
|
}
|
|
func (s *S3Client) Delete(ctx context.Context, key string) error {
|
|
_, err := s.request(ctx, "DELETE", key, "", nil)
|
|
return err
|
|
}
|
|
func (s *S3Client) Ready(ctx context.Context) error {
|
|
_, err := s.request(ctx, "HEAD", "", "", nil)
|
|
return err
|
|
}
|