createBucket creates the bucket if it doesn't exist yet. When using actual Amazon S3, and trying to create a bucket you already own, the API returns a proper ErrCodeBucketAlreadyOwnedByYou. So we can just try to create it immediately and ignore the ErrCodeBucketAlreadyOwnedByYou error. But Scalewa
(origS3 bool, svc *awss3.S3, createBucketInput awss3.CreateBucketInput, bucketName string)
| 247 | // which could also mean that someone else owns it, which would be an error. |
| 248 | // So in this case we must do it differently. |
| 249 | func createBucket(origS3 bool, svc *awss3.S3, createBucketInput awss3.CreateBucketInput, bucketName string) error { |
| 250 | if origS3 { |
| 251 | _, err := svc.CreateBucket(&createBucketInput) |
| 252 | if err != nil { |
| 253 | aerr, ok := err.(awserr.Error) |
| 254 | if !ok || aerr.Code() != awss3.ErrCodeBucketAlreadyOwnedByYou { |
| 255 | return err |
| 256 | } |
| 257 | } |
| 258 | } else { |
| 259 | listBucketsOutput, err := svc.ListBuckets(&awss3.ListBucketsInput{}) |
| 260 | if err != nil { |
| 261 | return err |
| 262 | } |
| 263 | ownsBucket := false |
| 264 | if listBucketsOutput.Buckets != nil { |
| 265 | for _, bucket := range listBucketsOutput.Buckets { |
| 266 | if *bucket.Name == bucketName { |
| 267 | ownsBucket = true |
| 268 | break |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | if !ownsBucket { |
| 273 | _, err = svc.CreateBucket(&createBucketInput) |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return nil |
| 281 | } |