Доработайте публикацию и сверку вложений по итогам ревью
This commit is contained in:
@@ -16,10 +16,13 @@ import (
|
||||
"github.com/minio/minio-go/v7/pkg/s3utils"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/platform/config"
|
||||
)
|
||||
|
||||
const MaxPageSize = 500
|
||||
|
||||
const codeBucketAlreadyOwnedByYou = "BucketAlreadyOwnedByYou"
|
||||
|
||||
type Store struct {
|
||||
client *minio.Core
|
||||
region string
|
||||
@@ -30,15 +33,15 @@ var _ attachment.Blobs = (*Store)(nil)
|
||||
|
||||
// New validates configuration without contacting S3, so text-only service startup
|
||||
// and readiness do not depend on object storage being reachable.
|
||||
func New(endpoint, region, bucket, accessKey, secretKey string) (*Store, error) {
|
||||
u, err := url.Parse(endpoint)
|
||||
func New(cfg config.S3) (*Store, error) {
|
||||
u, err := url.Parse(cfg.Endpoint)
|
||||
if err != nil || u == nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" {
|
||||
return nil, errors.New("s3: endpoint must be an http(s) origin without credentials, query or fragment")
|
||||
}
|
||||
if strings.TrimSpace(region) == "" || accessKey == "" || secretKey == "" {
|
||||
if strings.TrimSpace(cfg.Region) == "" || cfg.AccessKey == "" || cfg.SecretKey == "" {
|
||||
return nil, errors.New("s3: region and credentials are required")
|
||||
}
|
||||
if err := s3utils.CheckValidBucketNameStrict(bucket); err != nil {
|
||||
if err := s3utils.CheckValidBucketNameStrict(cfg.Bucket); err != nil {
|
||||
return nil, fmt.Errorf("s3: invalid bucket: %w", err)
|
||||
}
|
||||
transport := &http.Transport{
|
||||
@@ -56,9 +59,9 @@ func New(endpoint, region, bucket, accessKey, secretKey string) (*Store, error)
|
||||
DisableCompression: true,
|
||||
}
|
||||
client, err := minio.NewCore(u.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: u.Scheme == "https",
|
||||
Region: region,
|
||||
Region: cfg.Region,
|
||||
BucketLookup: minio.BucketLookupPath,
|
||||
Transport: transport,
|
||||
// A consumed upload stream cannot be replayed. Let the caller decide
|
||||
@@ -68,7 +71,7 @@ func New(endpoint, region, bucket, accessKey, secretKey string) (*Store, error)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3: client: %w", err)
|
||||
}
|
||||
return &Store{client: client, region: region, bucket: bucket}, nil
|
||||
return &Store{client: client, region: cfg.Region, bucket: cfg.Bucket}, nil
|
||||
}
|
||||
|
||||
// Bootstrap is used only by the admin init command, never by API startup.
|
||||
@@ -78,7 +81,7 @@ func (s *Store) Bootstrap(ctx context.Context) error {
|
||||
}
|
||||
err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: s.region})
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
if err != nil && code != "BucketAlreadyOwnedByYou" {
|
||||
if err != nil && code != codeBucketAlreadyOwnedByYou {
|
||||
return wrap("create bucket", err)
|
||||
}
|
||||
return s.WaitReady(ctx)
|
||||
|
||||
Reference in New Issue
Block a user