Commit 79e788e4 by Carl Bergquist Committed by GitHub

Merge pull request #9937 from saady/gcs-path

[GCS] Support for gcs path
parents 3a772c7f 767b460f
...@@ -495,3 +495,4 @@ public_url = ...@@ -495,3 +495,4 @@ public_url =
[external_image_storage.gcs] [external_image_storage.gcs]
key_file = key_file =
bucket = bucket =
path =
\ No newline at end of file
...@@ -438,3 +438,4 @@ log_queries = ...@@ -438,3 +438,4 @@ log_queries =
[external_image_storage.gcs] [external_image_storage.gcs]
;key_file = ;key_file =
;bucket = ;bucket =
;path =
\ No newline at end of file
...@@ -778,6 +778,9 @@ Service Account should have "Storage Object Writer" role. ...@@ -778,6 +778,9 @@ Service Account should have "Storage Object Writer" role.
### bucket name ### bucket name
Bucket Name on Google Cloud Storage. Bucket Name on Google Cloud Storage.
### path
Optional extra path inside bucket
## [alerting] ## [alerting]
### enabled ### enabled
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
...@@ -20,19 +21,22 @@ const ( ...@@ -20,19 +21,22 @@ const (
type GCSUploader struct { type GCSUploader struct {
keyFile string keyFile string
bucket string bucket string
path string
log log.Logger log log.Logger
} }
func NewGCSUploader(keyFile, bucket string) *GCSUploader { func NewGCSUploader(keyFile, bucket, path string) *GCSUploader {
return &GCSUploader{ return &GCSUploader{
keyFile: keyFile, keyFile: keyFile,
bucket: bucket, bucket: bucket,
path: path,
log: log.New("gcsuploader"), log: log.New("gcsuploader"),
} }
} }
func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string, error) { 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) u.log.Debug("Opening key file ", u.keyFile)
data, err := ioutil.ReadFile(u.keyFile) data, err := ioutil.ReadFile(u.keyFile)
......
...@@ -73,8 +73,9 @@ func NewImageUploader() (ImageUploader, error) { ...@@ -73,8 +73,9 @@ func NewImageUploader() (ImageUploader, error) {
keyFile := gcssec.Key("key_file").MustString("") keyFile := gcssec.Key("key_file").MustString("")
bucketName := gcssec.Key("bucket").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 return NopImageUploader{}, nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment