Externalize S3 config from main

This commit is contained in:
Lena Fuhrimann 2017-02-22 11:18:20 +01:00
parent b2dce0b8ce
commit a4088e1408
4 changed files with 47 additions and 33 deletions

View file

@ -1,10 +1,16 @@
# S3 Manager # S3 Manager
Manage S3 buckets from any provider. A Web UI to written in Go to manage S3 buckets from any provider.
## Environment Variables ## Environment Variables
* `S3_ACCESS_KEY_ID` * `S3_ACCESS_KEY_ID`: Required. Your S3 access key ID
* `S3_SECRET_ACCESS_KEY` * `S3_SECRET_ACCESS_KEY`: Required. Your S3 secret access key
* `S3_ENDPOINT`: Optional. In case you are using a different S3 provider than AWS. Defaults to `s3.amazonaws.com` * `S3_ENDPOINT`: Optional. In case you are using a different S3 provider than AWS. Defaults to `s3.amazonaws.com`
* `V2_SIGNING`: Optional. In case your S3 provider still uses V2 Signing, set this to `true` * `V2_SIGNING`: Optional. In case your S3 provider still uses V2 Signing, set this to `true`
## Run locally
1. Set all environment variables
1. Run `go build .`
1. Run the binary and visit <http://localhost:8080>

29
main.go
View file

@ -4,42 +4,13 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/minio/minio-go"
) )
var minioClient *minio.Client
func main() { func main() {
var err error
port := os.Getenv("PORT") port := os.Getenv("PORT")
if len(port) == 0 { if len(port) == 0 {
port = "8080" port = "8080"
} }
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
if len(s3AccessKeyID) == 0 {
log.Fatalln("Please set S3_ACCESS_KEY_ID")
}
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
if len(s3SecretAccessKey) == 0 {
log.Fatalln("Please set S3_SECRET_ACCESS_KEY")
}
s3Endpoint := os.Getenv("S3_ENDPOINT")
if len(s3Endpoint) == 0 {
s3Endpoint = "s3.amazonaws.com"
}
if os.Getenv("V2_SIGNING") == "true" {
minioClient, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
} else {
minioClient, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
}
if err != nil {
panic(err)
}
router := NewRouter() router := NewRouter()

View file

@ -5,7 +5,6 @@ applications:
buildpack: https://github.com/cloudfoundry/go-buildpack.git buildpack: https://github.com/cloudfoundry/go-buildpack.git
env: env:
GOVERSION: go1.7
S3_ACCESS_KEY_ID: "XXX" S3_ACCESS_KEY_ID: "XXX"
S3_SECRET_ACCESS_KEY: "xxx" S3_SECRET_ACCESS_KEY: "xxx"
S3_ENDPOINT: "ds31s3.swisscom.com" S3_ENDPOINT: "ds31s3.swisscom.com"

38
minio.go Normal file
View file

@ -0,0 +1,38 @@
package main
import (
"log"
"os"
minio "github.com/minio/minio-go"
)
var minioClient *minio.Client
func init() {
var err error
s3Endpoint := os.Getenv("S3_ENDPOINT")
if len(s3Endpoint) == 0 {
s3Endpoint = "s3.amazonaws.com"
}
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
if len(s3AccessKeyID) == 0 {
log.Fatalln("Please set S3_ACCESS_KEY_ID")
}
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
if len(s3SecretAccessKey) == 0 {
log.Fatalln("Please set S3_SECRET_ACCESS_KEY")
}
if os.Getenv("V2_SIGNING") == "true" {
minioClient, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
} else {
minioClient, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
}
if err != nil {
log.Fatalln(err)
}
}