Commit f78a2839 by Torkel Ödegaard

s3: support for alternative s3 bucket url syntax, closes #7871

parent f2e42692
...@@ -27,25 +27,15 @@ func NewImageUploader() (ImageUploader, error) { ...@@ -27,25 +27,15 @@ func NewImageUploader() (ImageUploader, error) {
return nil, err return nil, err
} }
bucket := 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("")
info, err := getRegionAndBucketFromUrl(bucketUrl)
region := "" if err != nil {
rBucket := regexp.MustCompile(`https?:\/\/(.*)\.s3(-([^.]+))?\.amazonaws\.com\/?`) return nil, err
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"
}
} }
return NewS3Uploader(region, bucket, "public-read", accessKey, secretKey), nil return NewS3Uploader(info.region, info.bucket, "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 {
...@@ -65,3 +55,37 @@ func NewImageUploader() (ImageUploader, error) { ...@@ -65,3 +55,37 @@ func NewImageUploader() (ImageUploader, error) {
return NopImageUploader{}, nil 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")
}
...@@ -10,14 +10,14 @@ import ( ...@@ -10,14 +10,14 @@ import (
func TestImageUploaderFactory(t *testing.T) { func TestImageUploaderFactory(t *testing.T) {
Convey("Can create image uploader for ", t, func() { Convey("Can create image uploader for ", t, func() {
Convey("S3ImageUploader", func() { Convey("S3ImageUploader config", func() {
var err error
setting.NewConfigContext(&setting.CommandLineArgs{ setting.NewConfigContext(&setting.CommandLineArgs{
HomePath: "../../../", HomePath: "../../../",
}) })
setting.ImageUploadProvider = "s3" setting.ImageUploadProvider = "s3"
Convey("with bucket url https://foo.bar.baz.s3-us-east-2.amazonaws.com", func() {
s3sec, err := setting.Cfg.GetSection("external_image_storage.s3") s3sec, err := setting.Cfg.GetSection("external_image_storage.s3")
s3sec.NewKey("bucket_url", "https://foo.bar.baz.s3-us-east-2.amazonaws.com") s3sec.NewKey("bucket_url", "https://foo.bar.baz.s3-us-east-2.amazonaws.com")
s3sec.NewKey("access_key", "access_key") s3sec.NewKey("access_key", "access_key")
...@@ -35,6 +35,43 @@ func TestImageUploaderFactory(t *testing.T) { ...@@ -35,6 +35,43 @@ func TestImageUploaderFactory(t *testing.T) {
So(original.secretKey, ShouldEqual, "secret_key") So(original.secretKey, ShouldEqual, "secret_key")
}) })
Convey("with bucket url https://s3.amazonaws.com/mybucket", func() {
s3sec, err := setting.Cfg.GetSection("external_image_storage.s3")
s3sec.NewKey("bucket_url", "https://s3.amazonaws.com/my.bucket.com")
s3sec.NewKey("access_key", "access_key")
s3sec.NewKey("secret_key", "secret_key")
uploader, err := NewImageUploader()
So(err, ShouldBeNil)
original, ok := uploader.(*S3Uploader)
So(ok, ShouldBeTrue)
So(original.region, ShouldEqual, "us-east-1")
So(original.bucket, ShouldEqual, "my.bucket.com")
So(original.accessKey, ShouldEqual, "access_key")
So(original.secretKey, ShouldEqual, "secret_key")
})
Convey("with bucket url https://s3-us-west-2.amazonaws.com/mybucket", func() {
s3sec, err := setting.Cfg.GetSection("external_image_storage.s3")
s3sec.NewKey("bucket_url", "https://s3-us-west-2.amazonaws.com/my.bucket.com")
s3sec.NewKey("access_key", "access_key")
s3sec.NewKey("secret_key", "secret_key")
uploader, err := NewImageUploader()
So(err, ShouldBeNil)
original, ok := uploader.(*S3Uploader)
So(ok, ShouldBeTrue)
So(original.region, ShouldEqual, "us-west-2")
So(original.bucket, ShouldEqual, "my.bucket.com")
So(original.accessKey, ShouldEqual, "access_key")
So(original.secretKey, ShouldEqual, "secret_key")
})
})
Convey("Webdav uploader", func() { Convey("Webdav uploader", func() {
var err error var err error
......
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