231 lines
5.3 KiB
Go
231 lines
5.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
var (
|
|
ErrDependencyUnavailable = errors.New("s3_unavailable")
|
|
ErrObjectNotFound = errors.New("object_not_found")
|
|
)
|
|
|
|
type Config struct {
|
|
Endpoint string
|
|
Region string
|
|
Bucket string
|
|
AccessKey string
|
|
SecretKey string
|
|
UseSSL bool
|
|
MaxUpload int64
|
|
MaxPixels int64
|
|
Grace time.Duration
|
|
}
|
|
|
|
type Client struct {
|
|
client *minio.Client
|
|
bucket string
|
|
cfg Config
|
|
}
|
|
|
|
func ConfigFromEnv() Config {
|
|
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
|
|
region := getenv("S3_REGION", "us-east-1")
|
|
bucket := getenv("S3_BUCKET", "messenger-attachments")
|
|
access := getenv("S3_ACCESS_KEY_ID", "")
|
|
secret := getenv("S3_SECRET_ACCESS_KEY", "")
|
|
|
|
useSSL := false
|
|
if v := getenv("S3_USE_SSL", "false"); v != "" {
|
|
parsed, err := strconv.ParseBool(v)
|
|
if err == nil {
|
|
useSSL = parsed
|
|
}
|
|
}
|
|
|
|
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
|
|
u, err := url.Parse(endpoint)
|
|
if err == nil {
|
|
endpoint = u.Host
|
|
if u.Scheme == "https" {
|
|
useSSL = true
|
|
}
|
|
}
|
|
}
|
|
|
|
maxUpload := getInt64("MAX_UPLOAD_BYTES", 10*1024*1024)
|
|
maxPixels := getInt64("MAX_IMAGE_PIXELS", 40_000_000)
|
|
grace := time.Duration(getInt64("ATTACHMENT_GRACE_SECONDS", 600)) * time.Second
|
|
|
|
return Config{
|
|
Endpoint: endpoint,
|
|
Region: region,
|
|
Bucket: bucket,
|
|
AccessKey: access,
|
|
SecretKey: secret,
|
|
UseSSL: useSSL,
|
|
MaxUpload: maxUpload,
|
|
MaxPixels: maxPixels,
|
|
Grace: grace,
|
|
}
|
|
}
|
|
|
|
func NewClient(cfg Config) (*Client, error) {
|
|
if cfg.Endpoint == "" {
|
|
return nil, errors.New("S3_ENDPOINT is required")
|
|
}
|
|
if cfg.Bucket == "" {
|
|
return nil, errors.New("S3_BUCKET is required")
|
|
}
|
|
if cfg.AccessKey == "" || cfg.SecretKey == "" {
|
|
return nil, errors.New("S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY are required")
|
|
}
|
|
|
|
client, err := minio.New(cfg.Endpoint, &minio.Options{
|
|
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
|
Secure: cfg.UseSSL,
|
|
Region: cfg.Region,
|
|
BucketLookup: minio.BucketLookupPath,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create minio client: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
exists, err := client.BucketExists(ctx, cfg.Bucket)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("check bucket exists: %w", err)
|
|
}
|
|
|
|
if !exists {
|
|
err = client.MakeBucket(ctx, cfg.Bucket, minio.MakeBucketOptions{Region: cfg.Region})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create bucket %s: %w", cfg.Bucket, err)
|
|
}
|
|
log.Printf("INFO: Successfully created bucket '%s'", cfg.Bucket)
|
|
}
|
|
|
|
return &Client{
|
|
client: client,
|
|
bucket: cfg.Bucket,
|
|
cfg: cfg,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) Config() Config {
|
|
return c.cfg
|
|
}
|
|
|
|
func (c *Client) PutObject(ctx context.Context, key string, data []byte, contentType string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
_, err := c.client.PutObject(
|
|
ctx,
|
|
c.bucket,
|
|
key,
|
|
bytes.NewReader(data),
|
|
int64(len(data)),
|
|
minio.PutObjectOptions{
|
|
ContentType: contentType,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: put object: %v", ErrDependencyUnavailable, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Object struct {
|
|
Body io.ReadCloser
|
|
Size int64
|
|
ContentType string
|
|
}
|
|
|
|
func (c *Client) GetObject(ctx context.Context, key string) (*Object, error) {
|
|
// ВАЖНО: не создаём новый контекст с таймаутом и не делаем defer cancel().
|
|
// Контекст HTTP-запроса должен жить, пока клиент читает поток (io.Copy).
|
|
// Он будет отменён автоматически при отключении клиента.
|
|
|
|
info, err := c.client.StatObject(ctx, c.bucket, key, minio.StatObjectOptions{})
|
|
if err != nil {
|
|
errResp := minio.ToErrorResponse(err)
|
|
if errResp.StatusCode == 404 || errResp.Code == "NoSuchKey" || errResp.Code == "NotFound" {
|
|
return nil, ErrObjectNotFound
|
|
}
|
|
return nil, fmt.Errorf("%w: stat object: %v", ErrDependencyUnavailable, err)
|
|
}
|
|
|
|
obj, err := c.client.GetObject(ctx, c.bucket, key, minio.GetObjectOptions{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: get object: %v", ErrDependencyUnavailable, err)
|
|
}
|
|
|
|
contentType := info.ContentType
|
|
if contentType == "" {
|
|
contentType = "application/octet-stream"
|
|
}
|
|
|
|
return &Object{
|
|
Body: obj,
|
|
Size: info.Size,
|
|
ContentType: contentType,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) DeleteObject(ctx context.Context, key string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
err := c.client.RemoveObject(ctx, c.bucket, key, minio.RemoveObjectOptions{})
|
|
if err != nil {
|
|
errResp := minio.ToErrorResponse(err)
|
|
|
|
if errResp.StatusCode == 404 ||
|
|
errResp.Code == "NoSuchKey" ||
|
|
errResp.Code == "NotFound" {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("%w: delete object: %v", ErrDependencyUnavailable, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getenv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getInt64(key string, fallback int64) int64 {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
|
|
parsed, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
|
|
return parsed
|
|
}
|