Skip to content

Commit

Permalink
fix: Amazon S3 region detection seems to be broken
Browse files Browse the repository at this point in the history
Close #22, Close #23
  • Loading branch information
develar committed Mar 3, 2020
1 parent f2b2c8b commit 05b559c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app-builder-bin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "app-builder-bin",
"description": "app-builder precompiled binaries",
"version": "3.5.3",
"version": "3.5.4",
"files": [
"*.js",
"mac",
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
return
}

var app = kingpin.New("app-builder", "app-builder").Version("3.5.3")
var app = kingpin.New("app-builder", "app-builder").Version("3.5.4")

node_modules.ConfigureCommand(app)
node_modules.ConfigureRebuildCommand(app)
Expand Down
26 changes: 12 additions & 14 deletions pkg/publisher/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"os"
"path"
"strings"
"time"

"github.com/alecthomas/kingpin"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/develar/app-builder/pkg/util"
"github.com/develar/errors"
Expand Down Expand Up @@ -66,9 +67,9 @@ func ConfigurePublishToS3Command(app *kingpin.Application) {
func configureResolveBucketLocationCommand(app *kingpin.Application) {
command := app.Command("get-bucket-location", "")
bucket := command.Flag("bucket", "").Required().String()
command.Action(func(context *kingpin.ParseContext) error {
requestContext, _ := util.CreateContext()
result, err := getBucketRegion(aws.NewConfig(), bucket, requestContext, createHttpClient())
command.Action(func(parseContext *kingpin.ParseContext) error {
requestContext, _ := util.CreateContextWithTimeout(30*time.Second)
result, err := getBucketRegion(aws.NewConfig(), *bucket, requestContext, createHttpClient())
if err != nil {
return err
}
Expand All @@ -81,7 +82,7 @@ func configureResolveBucketLocationCommand(app *kingpin.Application) {
})
}

func getBucketRegion(awsConfig *aws.Config, bucket *string, context context.Context, httpClient *http.Client) (string, error) {
func getBucketRegion(awsConfig *aws.Config, bucket string, context context.Context, httpClient *http.Client) (string, error) {
awsSession, err := session.NewSession(awsConfig, &aws.Config{
// any region required
Region: aws.String("us-east-1"),
Expand All @@ -91,17 +92,14 @@ func getBucketRegion(awsConfig *aws.Config, bucket *string, context context.Cont
return "", errors.WithStack(err)
}

client := s3.New(awsSession)
result, err := client.GetBucketLocationWithContext(context, &s3.GetBucketLocationInput{
Bucket: bucket,
})
result, err := s3manager.GetBucketRegion(context, awsSession, bucket, "")
if err != nil {
if awsError, ok := err.(awserr.Error); ok && awsError.Code() == "NotFound" {
return "", errors.Errorf("unable to find bucket %s's region not found", bucket)
}
return "", errors.WithStack(err)
}
if result == nil || result.LocationConstraint == nil || len(*result.LocationConstraint) == 0 {
return "us-east-1", nil
}
return *result.LocationConstraint, nil
return result, nil
}

func upload(options *ObjectOptions) error {
Expand Down Expand Up @@ -130,7 +128,7 @@ func upload(options *ObjectOptions) error {
awsConfig.Region = aws.String("us-east-1")
default:
// AWS SDK for Go requires region
region, err := getBucketRegion(awsConfig, options.bucket, publishContext, httpClient)
region, err := getBucketRegion(awsConfig, *options.bucket, publishContext, httpClient)
if err != nil {
return errors.WithStack(err)
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/util/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/develar/app-builder/pkg/log"
"go.uber.org/zap"
)

func CreateContext() (context.Context, context.CancelFunc) {
downloadContext, cancel := context.WithCancel(context.Background())
c, cancel := context.WithCancel(context.Background())
go onCancelSignal(cancel)
return downloadContext, cancel
return c, cancel
}

func CreateContextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
c, cancel := context.WithTimeout(context.Background(), timeout)
go onCancelSignal(cancel)
return c, cancel
}

func onCancelSignal(cancel context.CancelFunc) {
Expand Down

0 comments on commit 05b559c

Please sign in to comment.