support for s3 path (#9151)

This commit is contained in:
Ricard Clau 2017-10-04 21:04:20 +02:00 committed by Torkel Ödegaard
parent 0c31c7b106
commit e4541a7fd1
5 changed files with 18 additions and 3 deletions

View File

@ -479,6 +479,7 @@ provider =
bucket_url = bucket_url =
bucket = bucket =
region = region =
path =
access_key = access_key =
secret_key = secret_key =

View File

@ -424,6 +424,7 @@
[external_image_storage.s3] [external_image_storage.s3]
;bucket = ;bucket =
;region = ;region =
;path =
;access_key = ;access_key =
;secret_key = ;secret_key =

View File

@ -651,11 +651,16 @@ These options control how images should be made public so they can be shared on
You can choose between (s3, webdav, gcs). If left empty Grafana will ignore the upload action. You can choose between (s3, webdav, gcs). If left empty Grafana will ignore the upload action.
## [external_image_storage.s3] ## [external_image_storage.s3]
### bucket ### bucket
Bucket name for S3. e.g. grafana.snapshot Bucket name for S3. e.g. grafana.snapshot
### region ### region
Region name for S3. e.g. 'us-east-1', 'cn-north-1', etc Region name for S3. e.g. 'us-east-1', 'cn-north-1', etc
### path
Optional extra path inside bucket, useful to apply expiration policies
### bucket_url ### bucket_url
(for backward compatibility, only works when no bucket or region are configured) (for backward compatibility, only works when no bucket or region are configured)
Bucket URL for S3. AWS region can be specified within URL or defaults to 'us-east-1', e.g. Bucket URL for S3. AWS region can be specified within URL or defaults to 'us-east-1', e.g.

View File

@ -30,9 +30,15 @@ func NewImageUploader() (ImageUploader, error) {
bucket := s3sec.Key("bucket").MustString("") bucket := s3sec.Key("bucket").MustString("")
region := s3sec.Key("region").MustString("") region := s3sec.Key("region").MustString("")
path := s3sec.Key("path").MustString("")
bucketUrl := s3sec.Key("bucket_url").MustString("") bucketUrl := s3sec.Key("bucket_url").MustString("")
accessKey := s3sec.Key("access_key").MustString("") accessKey := s3sec.Key("access_key").MustString("")
secretKey := s3sec.Key("secret_key").MustString("") secretKey := s3sec.Key("secret_key").MustString("")
if path != "" && path[len(path)-1:] != "/" {
path += "/"
}
if bucket == "" || region == "" { if bucket == "" || region == "" {
info, err := getRegionAndBucketFromUrl(bucketUrl) info, err := getRegionAndBucketFromUrl(bucketUrl)
if err != nil { if err != nil {
@ -42,7 +48,7 @@ func NewImageUploader() (ImageUploader, error) {
region = info.region region = info.region
} }
return NewS3Uploader(region, bucket, "public-read", accessKey, secretKey), nil return NewS3Uploader(region, bucket, path, "public-read", accessKey, secretKey), nil
case "webdav": case "webdav":
webdavSec, err := setting.Cfg.GetSection("external_image_storage.webdav") webdavSec, err := setting.Cfg.GetSection("external_image_storage.webdav")
if err != nil { if err != nil {

View File

@ -19,16 +19,18 @@ import (
type S3Uploader struct { type S3Uploader struct {
region string region string
bucket string bucket string
path string
acl string acl string
secretKey string secretKey string
accessKey string accessKey string
log log.Logger log log.Logger
} }
func NewS3Uploader(region, bucket, acl, accessKey, secretKey string) *S3Uploader { func NewS3Uploader(region, bucket, path, acl, accessKey, secretKey string) *S3Uploader {
return &S3Uploader{ return &S3Uploader{
region: region, region: region,
bucket: bucket, bucket: bucket,
path: path,
acl: acl, acl: acl,
accessKey: accessKey, accessKey: accessKey,
secretKey: secretKey, secretKey: secretKey,
@ -56,7 +58,7 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string,
} }
s3_endpoint, _ := endpoints.DefaultResolver().EndpointFor("s3", u.region) s3_endpoint, _ := endpoints.DefaultResolver().EndpointFor("s3", u.region)
key := util.GetRandomString(20) + ".png" key := u.path + util.GetRandomString(20) + ".png"
image_url := s3_endpoint.URL + "/" + u.bucket + "/" + key image_url := s3_endpoint.URL + "/" + u.bucket + "/" + key
log.Debug("Uploading image to s3", "url = ", image_url) log.Debug("Uploading image to s3", "url = ", image_url)