mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
gofmt fixes
This commit is contained in:
parent
72d7c4d620
commit
1188f8df73
@ -1,56 +1,56 @@
|
||||
package imguploader
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"google.golang.org/api/option"
|
||||
"cloud.google.com/go/storage"
|
||||
"golang.org/x/net/context"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"cloud.google.com/go/storage"
|
||||
"fmt"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/api/option"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type GCPUploader struct {
|
||||
keyFile string
|
||||
bucket string
|
||||
log log.Logger
|
||||
keyFile string
|
||||
bucket string
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func NewGCPUploader(keyFile, bucket string) *GCPUploader {
|
||||
return &GCPUploader{
|
||||
keyFile: keyFile,
|
||||
bucket: bucket,
|
||||
log: log.New("gcpuploader"),
|
||||
}
|
||||
return &GCPUploader{
|
||||
keyFile: keyFile,
|
||||
bucket: bucket,
|
||||
log: log.New("gcpuploader"),
|
||||
}
|
||||
}
|
||||
|
||||
func (u *GCPUploader) Upload(imageDiskPath string) (string, error) {
|
||||
ctx := context.Background()
|
||||
ctx := context.Background()
|
||||
|
||||
client, err := storage.NewClient(ctx, option.WithServiceAccountFile(u.keyFile))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
client, err := storage.NewClient(ctx, option.WithServiceAccountFile(u.keyFile))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
key := util.GetRandomString(20) + ".png"
|
||||
log.Debug("Uploading image to GCP bucket = %s key = %s", u.bucket, key)
|
||||
key := util.GetRandomString(20) + ".png"
|
||||
log.Debug("Uploading image to GCP bucket = %s key = %s", u.bucket, key)
|
||||
|
||||
file, err := ioutil.ReadFile(imageDiskPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
file, err := ioutil.ReadFile(imageDiskPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
wc := client.Bucket(u.bucket).Object(key).NewWriter(ctx)
|
||||
wc.ContentType = "image/png"
|
||||
wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
|
||||
wc := client.Bucket(u.bucket).Object(key).NewWriter(ctx)
|
||||
wc.ContentType = "image/png"
|
||||
wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
|
||||
|
||||
if _, err := wc.Write(file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := wc.Write(file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := wc.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := wc.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", u.bucket, key), nil
|
||||
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", u.bucket, key), nil
|
||||
}
|
||||
|
@ -52,16 +52,16 @@ func NewImageUploader() (ImageUploader, error) {
|
||||
password := webdavSec.Key("password").String()
|
||||
|
||||
return NewWebdavImageUploader(url, username, password, public_url)
|
||||
case "gcp":
|
||||
gcpsec, err := setting.Cfg.GetSection("external_image_storage.gcp")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "gcp":
|
||||
gcpsec, err := setting.Cfg.GetSection("external_image_storage.gcp")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyFile := gcpsec.Key("key_file").MustString("")
|
||||
bucketName := gcpsec.Key("bucket").MustString("")
|
||||
keyFile := gcpsec.Key("key_file").MustString("")
|
||||
bucketName := gcpsec.Key("bucket").MustString("")
|
||||
|
||||
return NewGCPUploader(keyFile, bucketName), nil
|
||||
return NewGCPUploader(keyFile, bucketName), nil
|
||||
}
|
||||
|
||||
return NopImageUploader{}, nil
|
||||
|
@ -97,7 +97,7 @@ func TestImageUploaderFactory(t *testing.T) {
|
||||
So(original.password, ShouldEqual, "password")
|
||||
})
|
||||
|
||||
Convey("GCP uploader", func() {
|
||||
Convey("GCP uploader", func() {
|
||||
var err error
|
||||
|
||||
setting.NewConfigContext(&setting.CommandLineArgs{
|
||||
|
Loading…
Reference in New Issue
Block a user