forked from mtusi2026/mtusi2026lab4
feat: restricted S3 credentials, orphan reconciler, upload limits (grade 4)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
@@ -14,19 +15,30 @@ import (
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
region string
|
||||
}
|
||||
|
||||
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
|
||||
func EndpointHost(endpoint string) (string, bool) {
|
||||
host, secure := endpoint, false
|
||||
if parsed, err := url.Parse(endpoint); err == nil && parsed.Host != "" {
|
||||
host = parsed.Host
|
||||
secure = parsed.Scheme == "https"
|
||||
}
|
||||
client, err := minio.New(strings.TrimRight(host, "/"), &minio.Options{
|
||||
return strings.TrimRight(host, "/"), secure
|
||||
}
|
||||
|
||||
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
|
||||
host, secure := EndpointHost(endpoint)
|
||||
client, err := minio.New(host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||
Region: region,
|
||||
Secure: secure,
|
||||
@@ -56,17 +68,18 @@ func (s *Store) EnsureBucket(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
_, err := s.client.BucketExists(ctx, s.bucket)
|
||||
return s.wrap(err)
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return s.wrap(err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("%w: bucket is missing", domain.ErrUnavailable)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
|
||||
if err != nil && minio.ToErrorResponse(err).Code == "NoSuchBucket" {
|
||||
if ensureErr := s.EnsureBucket(ctx); ensureErr == nil {
|
||||
_, err = s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
|
||||
}
|
||||
}
|
||||
return s.wrap(err)
|
||||
}
|
||||
|
||||
@@ -86,6 +99,38 @@ func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return s.wrap(s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}))
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, prefix string, limit int) ([]ObjectInfo, bool, error) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
objects := make([]ObjectInfo, 0, 64)
|
||||
for info := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
||||
if info.Err != nil {
|
||||
return nil, false, s.wrap(info.Err)
|
||||
}
|
||||
if len(objects) >= limit {
|
||||
return objects, true, nil
|
||||
}
|
||||
objects = append(objects, ObjectInfo{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
|
||||
}
|
||||
return objects, false, nil
|
||||
}
|
||||
|
||||
func (s *Store) Buckets(ctx context.Context) ([]string, error) {
|
||||
buckets, err := s.client.ListBuckets(ctx)
|
||||
if err != nil {
|
||||
return nil, s.wrap(err)
|
||||
}
|
||||
names := make([]string, len(buckets))
|
||||
for i, bucket := range buckets {
|
||||
names[i] = bucket.Name
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (s *Store) RemoveBucket(ctx context.Context, bucket string) error {
|
||||
return s.wrap(s.client.RemoveBucket(ctx, bucket))
|
||||
}
|
||||
|
||||
func (s *Store) wrap(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user