diff --git a/backend_s3.go b/backend_s3.go index 3bfd1f9..ad71106 100644 --- a/backend_s3.go +++ b/backend_s3.go @@ -2,12 +2,14 @@ package main import ( "bytes" + "flag" "fmt" "image" "image/jpeg" "io" "log" "net/http" + "os" "path" "github.com/aws/aws-sdk-go/aws" @@ -17,8 +19,35 @@ import ( "github.com/aws/aws-sdk-go/service/s3/s3manager" ) +var ( + s3_endpoint string + s3_region = "us" + s3_bucket string + s3_access_key string + s3_secret_key string + s3_path_style bool +) + func init() { existing_backends = append(existing_backends, "s3") + + s3_endpoint, _ = os.LookupEnv("S3_ENDPOINT") + if region, ok := os.LookupEnv("S3_REGION"); ok { + s3_region = region + } + s3_bucket, _ = os.LookupEnv("S3_BUCKET") + s3_access_key, _ = os.LookupEnv("S3_ACCESS_KEY") + s3_secret_key, _ = os.LookupEnv("S3_SECRET_KEY") + if path_style, ok := os.LookupEnv("S3_PATH_STYLE"); ok { + s3_path_style = path_style == "1" || path_style == "ON" || path_style == "on" || path_style == "TRUE" || path_style == "true" || path_style == "yes" || path_style == "YES" + } + + flag.StringVar(&s3_endpoint, "s3-endpoint", s3_endpoint, "When using S3 backend, endpoint to use") + flag.StringVar(&s3_region, "s3-region", s3_region, "When using S3 backend, region to use") + flag.StringVar(&s3_bucket, "s3-bucket", s3_bucket, "When using S3 backend, bucket to use") + flag.StringVar(&s3_access_key, "s3-access-key", s3_access_key, "When using S3 backend, Access Key") + flag.StringVar(&s3_secret_key, "s3-secret-key", s3_secret_key, "When using S3 backend, Secret Key") + flag.BoolVar(&s3_path_style, "s3-path-style", s3_path_style, "When using S3 backend, force path style (when using minio)") } type S3FileBackend struct { diff --git a/main.go b/main.go index dbbe066..6832338 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "log" "net/http" "os" @@ -16,6 +17,8 @@ func main() { htpasswd_file := flag.String("htpasswd", "", "Admin passwords file, Apache htpasswd format") publishedImgDir := flag.String("publishedimgdir", "published/", "Directory where save published pictures") nextImgDir := flag.String("nextimgdir", "next/", "Directory where save pictures to review") + baseDir := flag.String("basedir", "images/", "Local base directory where find published and next dirs") + backend := flag.String("storage-backend", "local", fmt.Sprintf("Storage backend to use (between: %s)", existing_backends)) flag.StringVar(&ThumbsDir, "thumbsdir", "./images/thumbs/", "Directory where generate thumbs") flag.Parse() @@ -51,8 +54,17 @@ func main() { http.FileServer(Assets).ServeHTTP(w, r) }) + var storage_backend FileBackend + if backend == nil || *backend == "local" { + storage_backend = &LocalFileBackend{*baseDir} + } else if *backend == "s3" { + storage_backend = &S3FileBackend{s3_endpoint, s3_region, s3_bucket, s3_access_key, s3_secret_key, s3_path_style, *baseDir} + } else { + log.Fatalf("%q is not a valid storage backend.", *backend) + } + pe := &PictureExplorer{ - FileBackend: &LocalFileBackend{"./images/"}, + FileBackend: storage_backend, PublishedImgDir: *publishedImgDir, NextImgDir: *nextImgDir, }