mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
s3: support for alternative s3 bucket url syntax, closes #7871
This commit is contained in:
@@ -27,25 +27,15 @@ func NewImageUploader() (ImageUploader, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bucket := s3sec.Key("bucket_url").MustString("")
|
||||
bucketUrl := s3sec.Key("bucket_url").MustString("")
|
||||
accessKey := s3sec.Key("access_key").MustString("")
|
||||
secretKey := s3sec.Key("secret_key").MustString("")
|
||||
|
||||
region := ""
|
||||
rBucket := regexp.MustCompile(`https?:\/\/(.*)\.s3(-([^.]+))?\.amazonaws\.com\/?`)
|
||||
matches := rBucket.FindStringSubmatch(bucket)
|
||||
if len(matches) == 0 {
|
||||
return nil, fmt.Errorf("Could not find bucket setting for image.uploader.s3")
|
||||
} else {
|
||||
bucket = matches[1]
|
||||
if matches[3] != "" {
|
||||
region = matches[3]
|
||||
} else {
|
||||
region = "us-east-1"
|
||||
}
|
||||
info, err := getRegionAndBucketFromUrl(bucketUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewS3Uploader(region, bucket, "public-read", accessKey, secretKey), nil
|
||||
return NewS3Uploader(info.region, info.bucket, "public-read", accessKey, secretKey), nil
|
||||
case "webdav":
|
||||
webdavSec, err := setting.Cfg.GetSection("external_image_storage.webdav")
|
||||
if err != nil {
|
||||
@@ -65,3 +55,37 @@ func NewImageUploader() (ImageUploader, error) {
|
||||
|
||||
return NopImageUploader{}, nil
|
||||
}
|
||||
|
||||
type s3Info struct {
|
||||
region string
|
||||
bucket string
|
||||
}
|
||||
|
||||
func getRegionAndBucketFromUrl(url string) (*s3Info, error) {
|
||||
info := &s3Info{}
|
||||
urlRegex := regexp.MustCompile(`https?:\/\/(.*)\.s3(-([^.]+))?\.amazonaws\.com\/?`)
|
||||
matches := urlRegex.FindStringSubmatch(url)
|
||||
if len(matches) > 0 {
|
||||
info.bucket = matches[1]
|
||||
if matches[3] != "" {
|
||||
info.region = matches[3]
|
||||
} else {
|
||||
info.region = "us-east-1"
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
urlRegex2 := regexp.MustCompile(`https?:\/\/s3(-([^.]+))?\.amazonaws\.com\/(.*)?`)
|
||||
matches2 := urlRegex2.FindStringSubmatch(url)
|
||||
if len(matches2) > 0 {
|
||||
info.bucket = matches2[3]
|
||||
if matches2[2] != "" {
|
||||
info.region = matches2[2]
|
||||
} else {
|
||||
info.region = "us-east-1"
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Could not find bucket setting for image.uploader.s3")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user