Skip to content

Commit

Permalink
[#17] fixed issue with s3 url gen (#29)
Browse files Browse the repository at this point in the history
* [#17] fixed issue with s3 URL gen

if the key was not in the s3 bucket we would still generate a pre-signed
URL. Now we will first do a quick check and then only if the key exists
do we generate a pre-signed URL.
  • Loading branch information
gdey authored and ARolek committed Jun 8, 2019
1 parent 042b26e commit 5b95f4b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion atlante/filestore/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type timeout interface {
// timeout errors should have a Timeout() bool on it.
type ErrPath struct {
Filepath string
isIntermediate bool
IsIntermediate bool
FilestoreType string
Err error
}
Expand All @@ -39,6 +39,8 @@ func (err ErrPath) Timeout() bool {
return ok && t.Timeout()
}

func (err ErrPath) Error() string { return err.Err.Error() }

// FileWriter returns a writer object
type FileWriter interface {
// Writer should return an io.Writer that can be used to write the file to.
Expand Down
30 changes: 24 additions & 6 deletions atlante/filestore/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"github.com/gdey/errors"
cfgaws "github.com/go-spatial/maptoolkit/atlante/config/aws"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -88,10 +89,12 @@ func initFunc(cfg filestore.Config) (filestore.Provider, error) {
intermediate: intermediate,
intermediateBucket: intermediateBucket,
intermediateBasePath: intermediateBasePath,
s3: s3.New(sess),
uploader: s3manager.NewUploader(sess),
urlTimeout: time.Duration(urlTimeout) * time.Minute,
genPresignedURL: genPresigned,
}

if genPresigned {
p.s3 = s3.New(sess)
}

testPath := filepath.Join(p.basePath("upload_test"), "testdata")
Expand Down Expand Up @@ -122,7 +125,6 @@ type Provider struct {
intermediateBasePath string
uploader *s3manager.Uploader

genPresignedURL bool
// used for getting signed urls
s3 *s3.S3
// time for urls that are generated if zero, then default is used.
Expand Down Expand Up @@ -169,17 +171,33 @@ func (p Provider) bucketPath(group, filepth string, isIntermediate bool) (bucket

// PathURL will get a pre-signed URL from aws for supported files.
func (p Provider) PathURL(group string, filepth string, isIntermediate bool) (*url.URL, error) {
if !p.genPresignedURL || p.s3 == nil {
if p.s3 == nil {
return nil, filestore.ErrUnsupportedOperation
}
// We don't support intermediate files for this operation
if !p.intermediate && isIntermediate {
return nil, filestore.ErrUnsupportedOperation
}
bucket, key := p.bucketPath(group, filepth, isIntermediate)
abucket, akey := aws.String(bucket), aws.String(key)

// Check to see if the key exists.
headObjInput := &s3.HeadObjectInput{
Bucket: abucket,
Key: akey,
}
if _, err := p.s3.HeadObject(headObjInput); err != nil {
return nil, filestore.ErrPath{
Filepath: filepth,
IsIntermediate: isIntermediate,
FilestoreType: TYPE,
Err: errors.String("file does not exist"),
}
}

req, _ := p.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Bucket: abucket,
Key: akey,
})
urlStr, err := req.Presign(p.urlTimeout)
if err != nil {
Expand Down

0 comments on commit 5b95f4b

Please sign in to comment.