bucket_location.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package s3
  2. import (
  3. "io/ioutil"
  4. "regexp"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/aws/awserr"
  7. "github.com/aws/aws-sdk-go/aws/awsutil"
  8. "github.com/aws/aws-sdk-go/aws/request"
  9. )
  10. var reBucketLocation = regexp.MustCompile(`>([^<>]+)<\/Location`)
  11. // NormalizeBucketLocation is a utility function which will update the
  12. // passed in value to always be a region ID. Generally this would be used
  13. // with GetBucketLocation API operation.
  14. //
  15. // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
  16. //
  17. // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
  18. // for more information on the values that can be returned.
  19. func NormalizeBucketLocation(loc string) string {
  20. switch loc {
  21. case "":
  22. loc = "us-east-1"
  23. case "EU":
  24. loc = "eu-west-1"
  25. }
  26. return loc
  27. }
  28. // NormalizeBucketLocationHandler is a request handler which will update the
  29. // GetBucketLocation's result LocationConstraint value to always be a region ID.
  30. //
  31. // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
  32. //
  33. // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
  34. // for more information on the values that can be returned.
  35. //
  36. // req, result := svc.GetBucketLocationRequest(&s3.GetBucketLocationInput{
  37. // Bucket: aws.String(bucket),
  38. // })
  39. // req.Handlers.Unmarshal.PushBackNamed(NormalizeBucketLocationHandler)
  40. // err := req.Send()
  41. var NormalizeBucketLocationHandler = request.NamedHandler{
  42. Name: "awssdk.s3.NormalizeBucketLocation",
  43. Fn: func(req *request.Request) {
  44. if req.Error != nil {
  45. return
  46. }
  47. out := req.Data.(*GetBucketLocationOutput)
  48. loc := NormalizeBucketLocation(aws.StringValue(out.LocationConstraint))
  49. out.LocationConstraint = aws.String(loc)
  50. },
  51. }
  52. // WithNormalizeBucketLocation is a request option which will update the
  53. // GetBucketLocation's result LocationConstraint value to always be a region ID.
  54. //
  55. // Replaces empty string with "us-east-1", and "EU" with "eu-west-1".
  56. //
  57. // See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
  58. // for more information on the values that can be returned.
  59. //
  60. // result, err := svc.GetBucketLocationWithContext(ctx,
  61. // &s3.GetBucketLocationInput{
  62. // Bucket: aws.String(bucket),
  63. // },
  64. // s3.WithNormalizeBucketLocation,
  65. // )
  66. func WithNormalizeBucketLocation(r *request.Request) {
  67. r.Handlers.Unmarshal.PushBackNamed(NormalizeBucketLocationHandler)
  68. }
  69. func buildGetBucketLocation(r *request.Request) {
  70. if r.DataFilled() {
  71. out := r.Data.(*GetBucketLocationOutput)
  72. b, err := ioutil.ReadAll(r.HTTPResponse.Body)
  73. if err != nil {
  74. r.Error = awserr.New("SerializationError", "failed reading response body", err)
  75. return
  76. }
  77. match := reBucketLocation.FindSubmatch(b)
  78. if len(match) > 1 {
  79. loc := string(match[1])
  80. out.LocationConstraint = aws.String(loc)
  81. }
  82. }
  83. }
  84. func populateLocationConstraint(r *request.Request) {
  85. if r.ParamsFilled() && aws.StringValue(r.Config.Region) != "us-east-1" {
  86. in := r.Params.(*CreateBucketInput)
  87. if in.CreateBucketConfiguration == nil {
  88. r.Params = awsutil.CopyOf(r.Params)
  89. in = r.Params.(*CreateBucketInput)
  90. in.CreateBucketConfiguration = &CreateBucketConfiguration{
  91. LocationConstraint: r.Config.Region,
  92. }
  93. }
  94. }
  95. }