Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
be5c4f47
Commit
be5c4f47
authored
Mar 20, 2017
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
s3: support for alternative s3 bucket url syntax, closes #7871
parent
3f76d76b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
17 deletions
+78
-17
pkg/components/imguploader/imguploader.go
+39
-15
pkg/components/imguploader/imguploader_test.go
+39
-2
No files found.
pkg/components/imguploader/imguploader.go
View file @
be5c4f47
...
@@ -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
(
""
)
bucket
Url
:=
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"
)
}
pkg/components/imguploader/imguploader_test.go
View file @
be5c4f47
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment