7 "github.com/aws/aws-sdk-go/aws"
8 "github.com/aws/aws-sdk-go/aws/awserr"
9 "github.com/aws/aws-sdk-go/aws/awsutil"
10 "github.com/aws/aws-sdk-go/aws/request"
13 var reBucketLocation = regexp.MustCompile(`>([^<>]+)<\/Location`)
15 // NormalizeBucketLocation is a utility function which will update the
16 // passed in value to always be a region ID. Generally this would be used
17 // with GetBucketLocation API operation.
19 // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
21 // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
22 // for more information on the values that can be returned.
23 func NormalizeBucketLocation(loc string) string {
34 // NormalizeBucketLocationHandler is a request handler which will update the
35 // GetBucketLocation's result LocationConstraint value to always be a region ID.
37 // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
39 // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
40 // for more information on the values that can be returned.
42 // req, result := svc.GetBucketLocationRequest(&s3.GetBucketLocationInput{
43 // Bucket: aws.String(bucket),
45 // req.Handlers.Unmarshal.PushBackNamed(NormalizeBucketLocationHandler)
47 var NormalizeBucketLocationHandler = request.NamedHandler{
48 Name: "awssdk.s3.NormalizeBucketLocation",
49 Fn: func(req *request.Request) {
54 out := req.Data.(*GetBucketLocationOutput)
55 loc := NormalizeBucketLocation(aws.StringValue(out.LocationConstraint))
56 out.LocationConstraint = aws.String(loc)
60 // WithNormalizeBucketLocation is a request option which will update the
61 // GetBucketLocation's result LocationConstraint value to always be a region ID.
63 // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
65 // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
66 // for more information on the values that can be returned.
68 // result, err := svc.GetBucketLocationWithContext(ctx,
69 // &s3.GetBucketLocationInput{
70 // Bucket: aws.String(bucket),
72 // s3.WithNormalizeBucketLocation,
74 func WithNormalizeBucketLocation(r *request.Request) {
75 r.Handlers.Unmarshal.PushBackNamed(NormalizeBucketLocationHandler)
78 func buildGetBucketLocation(r *request.Request) {
80 out := r.Data.(*GetBucketLocationOutput)
81 b, err := ioutil.ReadAll(r.HTTPResponse.Body)
83 r.Error = awserr.New("SerializationError", "failed reading response body", err)
87 match := reBucketLocation.FindSubmatch(b)
89 loc := string(match[1])
90 out.LocationConstraint = aws.String(loc)
95 func populateLocationConstraint(r *request.Request) {
96 if r.ParamsFilled() && aws.StringValue(r.Config.Region) != "us-east-1" {
97 in := r.Params.(*CreateBucketInput)
98 if in.CreateBucketConfiguration == nil {
99 r.Params = awsutil.CopyOf(r.Params)
100 in = r.Params.(*CreateBucketInput)
101 in.CreateBucketConfiguration = &CreateBucketConfiguration{
102 LocationConstraint: r.Config.Region,