fix stuff
This commit is contained in:
@@ -12,6 +12,7 @@ SESSION_SECRET=local-development-session-secret-change-me
|
|||||||
COOKIE_SECURE=false
|
COOKIE_SECURE=false
|
||||||
|
|
||||||
STORAGE_ENDPOINT=localhost:9000
|
STORAGE_ENDPOINT=localhost:9000
|
||||||
|
STORAGE_PUBLIC_ENDPOINT=
|
||||||
STORAGE_ACCESS_KEY=minioadmin
|
STORAGE_ACCESS_KEY=minioadmin
|
||||||
STORAGE_SECRET_KEY=minioadmin
|
STORAGE_SECRET_KEY=minioadmin
|
||||||
STORAGE_BUCKET=gallery-media
|
STORAGE_BUCKET=gallery-media
|
||||||
|
|||||||
@@ -46,10 +46,12 @@ func main() {
|
|||||||
|
|
||||||
objectStorage, err := storage.NewMinIO(storage.Config{
|
objectStorage, err := storage.NewMinIO(storage.Config{
|
||||||
Endpoint: cfg.StorageEndpoint,
|
Endpoint: cfg.StorageEndpoint,
|
||||||
|
PublicEndpoint: cfg.StoragePublicEndpoint,
|
||||||
AccessKey: cfg.StorageAccessKey,
|
AccessKey: cfg.StorageAccessKey,
|
||||||
SecretKey: cfg.StorageSecretKey,
|
SecretKey: cfg.StorageSecretKey,
|
||||||
Bucket: cfg.StorageBucket,
|
Bucket: cfg.StorageBucket,
|
||||||
UseSSL: cfg.StorageUseSSL,
|
UseSSL: cfg.StorageUseSSL,
|
||||||
|
PublicUseSSL: true,
|
||||||
CORSOrigins: cfg.CORSOrigin,
|
CORSOrigins: cfg.CORSOrigin,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type Config struct {
|
|||||||
SessionSecret string
|
SessionSecret string
|
||||||
CookieSecure bool
|
CookieSecure bool
|
||||||
StorageEndpoint string
|
StorageEndpoint string
|
||||||
|
StoragePublicEndpoint string
|
||||||
StorageAccessKey string
|
StorageAccessKey string
|
||||||
StorageSecretKey string
|
StorageSecretKey string
|
||||||
StorageBucket string
|
StorageBucket string
|
||||||
@@ -40,6 +41,7 @@ func Load() Config {
|
|||||||
SessionSecret: envOrDefault("SESSION_SECRET", "local-development-session-secret-change-me"),
|
SessionSecret: envOrDefault("SESSION_SECRET", "local-development-session-secret-change-me"),
|
||||||
CookieSecure: parseBoolEnv("COOKIE_SECURE", false),
|
CookieSecure: parseBoolEnv("COOKIE_SECURE", false),
|
||||||
StorageEndpoint: envOrDefault("STORAGE_ENDPOINT", "localhost:9000"),
|
StorageEndpoint: envOrDefault("STORAGE_ENDPOINT", "localhost:9000"),
|
||||||
|
StoragePublicEndpoint: envOrDefault("STORAGE_PUBLIC_ENDPOINT", ""),
|
||||||
StorageAccessKey: envOrDefault("STORAGE_ACCESS_KEY", "minioadmin"),
|
StorageAccessKey: envOrDefault("STORAGE_ACCESS_KEY", "minioadmin"),
|
||||||
StorageSecretKey: envOrDefault("STORAGE_SECRET_KEY", "minioadmin"),
|
StorageSecretKey: envOrDefault("STORAGE_SECRET_KEY", "minioadmin"),
|
||||||
StorageBucket: envOrDefault("STORAGE_BUCKET", "gallery-media"),
|
StorageBucket: envOrDefault("STORAGE_BUCKET", "gallery-media"),
|
||||||
|
|||||||
@@ -14,10 +14,12 @@ import (
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
PublicEndpoint string
|
||||||
AccessKey string
|
AccessKey string
|
||||||
SecretKey string
|
SecretKey string
|
||||||
Bucket string
|
Bucket string
|
||||||
UseSSL bool
|
UseSSL bool
|
||||||
|
PublicUseSSL bool
|
||||||
CORSOrigins string
|
CORSOrigins string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +42,7 @@ type Storage interface {
|
|||||||
|
|
||||||
type MinIO struct {
|
type MinIO struct {
|
||||||
client *minio.Client
|
client *minio.Client
|
||||||
|
publicClient *minio.Client
|
||||||
bucket string
|
bucket string
|
||||||
corsOrigins string
|
corsOrigins string
|
||||||
}
|
}
|
||||||
@@ -55,7 +58,24 @@ func NewMinIO(config Config) (*MinIO, error) {
|
|||||||
if config.Bucket == "" {
|
if config.Bucket == "" {
|
||||||
return nil, fmt.Errorf("object storage bucket is required")
|
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 {
|
func (s *MinIO) EnsureBucket(ctx context.Context) error {
|
||||||
@@ -63,15 +83,14 @@ func (s *MinIO) EnsureBucket(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("check object storage bucket: %w", err)
|
return fmt.Errorf("check object storage bucket: %w", err)
|
||||||
}
|
}
|
||||||
if exists {
|
if !exists {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
||||||
response := minio.ToErrorResponse(err)
|
response := minio.ToErrorResponse(err)
|
||||||
if response.Code != "BucketAlreadyExists" && response.Code != "BucketAlreadyOwnedByYou" {
|
if response.Code != "BucketAlreadyExists" && response.Code != "BucketAlreadyOwnedByYou" {
|
||||||
return fmt.Errorf("create object storage bucket: %w", err)
|
return fmt.Errorf("create object storage bucket: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
origins := make([]string, 0)
|
origins := make([]string, 0)
|
||||||
for _, origin := range strings.Split(s.corsOrigins, ",") {
|
for _, origin := range strings.Split(s.corsOrigins, ",") {
|
||||||
if value := strings.TrimSpace(origin); value != "" {
|
if value := strings.TrimSpace(origin); value != "" {
|
||||||
@@ -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) {
|
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 {
|
if err != nil {
|
||||||
return "", fmt.Errorf("create upload URL: %w", err)
|
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) {
|
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 {
|
if err != nil {
|
||||||
return "", fmt.Errorf("create download URL: %w", err)
|
return "", fmt.Errorf("create download URL: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ services:
|
|||||||
SESSION_SECRET: "${SESSION_SECRET:-docker-compose-session-secret-change-me}"
|
SESSION_SECRET: "${SESSION_SECRET:-docker-compose-session-secret-change-me}"
|
||||||
COOKIE_SECURE: "${COOKIE_SECURE:-false}"
|
COOKIE_SECURE: "${COOKIE_SECURE:-false}"
|
||||||
STORAGE_ENDPOINT: "minio:9000"
|
STORAGE_ENDPOINT: "minio:9000"
|
||||||
|
STORAGE_PUBLIC_ENDPOINT: ""
|
||||||
STORAGE_ACCESS_KEY: "${STORAGE_ACCESS_KEY:-minioadmin}"
|
STORAGE_ACCESS_KEY: "${STORAGE_ACCESS_KEY:-minioadmin}"
|
||||||
STORAGE_SECRET_KEY: "${STORAGE_SECRET_KEY:-minioadmin}"
|
STORAGE_SECRET_KEY: "${STORAGE_SECRET_KEY:-minioadmin}"
|
||||||
STORAGE_BUCKET: "${STORAGE_BUCKET:-gallery-media}"
|
STORAGE_BUCKET: "${STORAGE_BUCKET:-gallery-media}"
|
||||||
|
|||||||
Reference in New Issue
Block a user