From 37027396caeb9f94e29c0af28fb96b507948f310 Mon Sep 17 00:00:00 2001 From: Khairul Hidayat Date: Sun, 13 Oct 2024 22:54:47 +0000 Subject: [PATCH] fix: make s3_region configurable to support values other than "garage" --- backend/main.go | 2 +- backend/router/browse.go | 2 +- backend/utils/garage.go | 14 +++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/main.go b/backend/main.go index 6642233..da5deef 100644 --- a/backend/main.go +++ b/backend/main.go @@ -16,7 +16,7 @@ func main() { utils.InitCacheManager() if err := utils.Garage.LoadConfig(); err != nil { - log.Fatal("Failed to load config! ", err) + log.Println("Cannot load garage config!", err) } http.Handle("/api/", http.StripPrefix("/api", router.HandleApiRouter())) diff --git a/backend/router/browse.go b/backend/router/browse.go index 9b1e469..fb33ad0 100644 --- a/backend/router/browse.go +++ b/backend/router/browse.go @@ -300,7 +300,7 @@ func getS3Client(bucket string) (*s3.Client, error) { awsConfig := aws.Config{ Credentials: creds, - Region: "garage", + Region: utils.Garage.GetS3Region(), BaseEndpoint: aws.String(utils.Garage.GetS3Endpoint()), } diff --git a/backend/utils/garage.go b/backend/utils/garage.go index 9d620e0..bde29ab 100644 --- a/backend/utils/garage.go +++ b/backend/utils/garage.go @@ -3,6 +3,7 @@ package utils import ( "bytes" "encoding/json" + "errors" "fmt" "io" "khairul169/garage-webui/schema" @@ -73,6 +74,17 @@ func (g *garage) GetS3Endpoint() string { return endpoint } +func (g *garage) GetS3Region() string { + endpoint := os.Getenv("S3_REGION") + if len(endpoint) > 0 { + return endpoint + } + if len(g.Config.S3API.S3Region) == 0 { + return "garage" + } + return g.Config.S3API.S3Region +} + func (g *garage) GetAdminKey() string { key := os.Getenv("API_ADMIN_KEY") if len(key) > 0 { @@ -153,7 +165,7 @@ func (g *garage) Fetch(url string, options *FetchOptions) ([]byte, error) { message = fmt.Sprintf("%v", data["message"]) } - return nil, fmt.Errorf(message) + return nil, errors.New(message) } body, err := io.ReadAll(res.Body)