fix stuff
This commit is contained in:
@@ -13,12 +13,14 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
Bucket string
|
||||
UseSSL bool
|
||||
CORSOrigins string
|
||||
Endpoint string
|
||||
PublicEndpoint string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
Bucket string
|
||||
UseSSL bool
|
||||
PublicUseSSL bool
|
||||
CORSOrigins string
|
||||
}
|
||||
|
||||
type ObjectInfo struct {
|
||||
@@ -39,9 +41,10 @@ type Storage interface {
|
||||
}
|
||||
|
||||
type MinIO struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
corsOrigins string
|
||||
client *minio.Client
|
||||
publicClient *minio.Client
|
||||
bucket string
|
||||
corsOrigins string
|
||||
}
|
||||
|
||||
func NewMinIO(config Config) (*MinIO, error) {
|
||||
@@ -55,7 +58,24 @@ func NewMinIO(config Config) (*MinIO, error) {
|
||||
if config.Bucket == "" {
|
||||
return nil, fmt.Errorf("object storage bucket is required")
|
||||
}
|
||||
return &MinIO{client: client, bucket: config.Bucket, corsOrigins: config.CORSOrigins}, nil
|
||||
|
||||
publicClient := client
|
||||
if config.PublicEndpoint != "" {
|
||||
publicClient, err = minio.New(config.PublicEndpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.AccessKey, config.SecretKey, ""),
|
||||
Secure: config.PublicUseSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create public object storage client: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &MinIO{
|
||||
client: client,
|
||||
publicClient: publicClient,
|
||||
bucket: config.Bucket,
|
||||
corsOrigins: config.CORSOrigins,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MinIO) EnsureBucket(ctx context.Context) error {
|
||||
@@ -63,13 +83,12 @@ func (s *MinIO) EnsureBucket(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("check object storage bucket: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
||||
response := minio.ToErrorResponse(err)
|
||||
if response.Code != "BucketAlreadyExists" && response.Code != "BucketAlreadyOwnedByYou" {
|
||||
return fmt.Errorf("create object storage bucket: %w", err)
|
||||
if !exists {
|
||||
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
||||
response := minio.ToErrorResponse(err)
|
||||
if response.Code != "BucketAlreadyExists" && response.Code != "BucketAlreadyOwnedByYou" {
|
||||
return fmt.Errorf("create object storage bucket: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
origins := make([]string, 0)
|
||||
@@ -95,7 +114,7 @@ func (s *MinIO) EnsureBucket(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (s *MinIO) CreateUploadURL(ctx context.Context, key, _ string, expiry time.Duration) (string, error) {
|
||||
url, err := s.client.PresignedPutObject(ctx, s.bucket, key, expiry)
|
||||
url, err := s.publicClient.PresignedPutObject(ctx, s.bucket, key, expiry)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create upload URL: %w", err)
|
||||
}
|
||||
@@ -103,7 +122,7 @@ func (s *MinIO) CreateUploadURL(ctx context.Context, key, _ string, expiry time.
|
||||
}
|
||||
|
||||
func (s *MinIO) CreateDownloadURL(ctx context.Context, key string, expiry time.Duration) (string, error) {
|
||||
url, err := s.client.PresignedGetObject(ctx, s.bucket, key, expiry, nil)
|
||||
url, err := s.publicClient.PresignedGetObject(ctx, s.bucket, key, expiry, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create download URL: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user