From 767b460ff1929d55b78287152feae48b9f432ddf Mon Sep 17 00:00:00 2001 From: saady Date: Mon, 20 Nov 2017 02:26:46 +0000 Subject: [PATCH] [GCS] Support for gcs path --- conf/defaults.ini | 1 + conf/sample.ini | 1 + docs/sources/installation/configuration.md | 3 +++ pkg/components/imguploader/gcsuploader.go | 8 ++++++-- pkg/components/imguploader/imguploader.go | 3 ++- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index 2c3064ecf22..a145d57482b 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -495,3 +495,4 @@ public_url = [external_image_storage.gcs] key_file = bucket = +path = \ No newline at end of file diff --git a/conf/sample.ini b/conf/sample.ini index 85e74451fef..233a97deef8 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -438,3 +438,4 @@ log_queries = [external_image_storage.gcs] ;key_file = ;bucket = +;path = \ No newline at end of file diff --git a/docs/sources/installation/configuration.md b/docs/sources/installation/configuration.md index c044b3d73dc..483774f94f5 100644 --- a/docs/sources/installation/configuration.md +++ b/docs/sources/installation/configuration.md @@ -778,6 +778,9 @@ Service Account should have "Storage Object Writer" role. ### bucket name Bucket Name on Google Cloud Storage. +### path +Optional extra path inside bucket + ## [alerting] ### enabled diff --git a/pkg/components/imguploader/gcsuploader.go b/pkg/components/imguploader/gcsuploader.go index 2271cec7db0..cb1ae19059d 100644 --- a/pkg/components/imguploader/gcsuploader.go +++ b/pkg/components/imguploader/gcsuploader.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "os" + "path" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/util" @@ -20,19 +21,22 @@ const ( type GCSUploader struct { keyFile string bucket string + path string log log.Logger } -func NewGCSUploader(keyFile, bucket string) *GCSUploader { +func NewGCSUploader(keyFile, bucket, path string) *GCSUploader { return &GCSUploader{ keyFile: keyFile, bucket: bucket, + path: path, log: log.New("gcsuploader"), } } func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string, error) { - key := util.GetRandomString(20) + ".png" + fileName := util.GetRandomString(20) + ".png" + key := path.Join(u.path, fileName) u.log.Debug("Opening key file ", u.keyFile) data, err := ioutil.ReadFile(u.keyFile) diff --git a/pkg/components/imguploader/imguploader.go b/pkg/components/imguploader/imguploader.go index 728614735d0..fd14b5d6739 100644 --- a/pkg/components/imguploader/imguploader.go +++ b/pkg/components/imguploader/imguploader.go @@ -73,8 +73,9 @@ func NewImageUploader() (ImageUploader, error) { keyFile := gcssec.Key("key_file").MustString("") bucketName := gcssec.Key("bucket").MustString("") + path := gcssec.Key("path").MustString("") - return NewGCSUploader(keyFile, bucketName), nil + return NewGCSUploader(keyFile, bucketName, path), nil } return NopImageUploader{}, nil