mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
minor code adjusetments
This commit is contained in:
parent
c7698a09ed
commit
16b1c0e63e
@ -2,7 +2,6 @@ package imguploader
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -13,6 +12,11 @@ import (
|
|||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
tokenUrl string = "https://www.googleapis.com/auth/devstorage.read_write"
|
||||||
|
uploadUrl string = "https://www.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s&predefinedAcl=publicRead"
|
||||||
|
)
|
||||||
|
|
||||||
type GCSUploader struct {
|
type GCSUploader struct {
|
||||||
keyFile string
|
keyFile string
|
||||||
bucket string
|
bucket string
|
||||||
@ -30,24 +34,20 @@ func NewGCSUploader(keyFile, bucket string) *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"
|
key := util.GetRandomString(20) + ".png"
|
||||||
|
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Creating JWT conf")
|
u.log.Debug("Creating JWT conf")
|
||||||
|
conf, err := google.JWTConfigFromJSON(data, tokenUrl)
|
||||||
conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/devstorage.read_write")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Creating HTTP client")
|
u.log.Debug("Creating HTTP client")
|
||||||
|
|
||||||
client := conf.Client(ctx)
|
client := conf.Client(ctx)
|
||||||
|
|
||||||
err = u.uploadFile(client, imageDiskPath, key)
|
err = u.uploadFile(client, imageDiskPath, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -57,20 +57,15 @@ func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *GCSUploader) uploadFile(client *http.Client, imageDiskPath, key string) error {
|
func (u *GCSUploader) uploadFile(client *http.Client, imageDiskPath, key string) error {
|
||||||
log.Debug("Opening image file ", imageDiskPath)
|
u.log.Debug("Opening image file ", imageDiskPath)
|
||||||
|
|
||||||
fileReader, err := os.Open(imageDiskPath)
|
fileReader, err := os.Open(imageDiskPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
reqUrl := fmt.Sprintf(
|
reqUrl := fmt.Sprintf(uploadUrl, u.bucket, key)
|
||||||
"https://www.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s&predefinedAcl=publicRead",
|
u.log.Debug("Request URL: ", reqUrl)
|
||||||
u.bucket,
|
|
||||||
key,
|
|
||||||
)
|
|
||||||
|
|
||||||
log.Debug("Request URL: ", reqUrl)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", reqUrl, fileReader)
|
req, err := http.NewRequest("POST", reqUrl, fileReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -78,18 +73,15 @@ func (u *GCSUploader) uploadFile(client *http.Client, imageDiskPath, key string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("Content-Type", "image/png")
|
req.Header.Add("Content-Type", "image/png")
|
||||||
|
u.log.Debug("Sending POST request to GCS")
|
||||||
log.Debug("Sending POST request to GCS")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("GCS API response header", resp.Header)
|
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
return errors.New(fmt.Sprintf("GCS response status code %d", resp.StatusCode))
|
return fmt.Errorf("GCS response status code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user