-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Vlasic
committed
Jul 8, 2021
1 parent
e7bf0f1
commit 46791ed
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"github.com/aws/aws-sdk-go-v2/service/s3/types" | ||
) | ||
|
||
type SDK struct { | ||
config aws.Config | ||
} | ||
|
||
func NewSDK() (*SDK, error) { | ||
config, err := config.LoadDefaultConfig(context.TODO()) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load SDK configuration - %v", err) | ||
} | ||
|
||
return &SDK{ | ||
config: config, | ||
}, nil | ||
} | ||
|
||
func (s *SDK) DefaultRegion() string { | ||
return s.config.Region | ||
} | ||
|
||
func (s *SDK) CreateS3Bucket(name, region string) error { | ||
client := s3.NewFromConfig(s.config) | ||
|
||
lc := s.BucketLocationConstraintForRegion(region) | ||
if lc == "" { | ||
return fmt.Errorf("region is not one of the known values - %s", region) | ||
} | ||
|
||
cbi := &s3.CreateBucketInput{ | ||
Bucket: aws.String(name), | ||
CreateBucketConfiguration: &types.CreateBucketConfiguration{ | ||
LocationConstraint: lc, | ||
}, | ||
} | ||
|
||
_, err := client.CreateBucket(context.TODO(), cbi) | ||
if err != nil { | ||
return fmt.Errorf("could not create bucket %s in %s - %v", name, region, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *SDK) DeleteS3Bucket(name string) error { | ||
client := s3.NewFromConfig(s.config) | ||
|
||
dbi := &s3.DeleteBucketInput{ | ||
Bucket: aws.String(name), | ||
} | ||
|
||
_, err := client.DeleteBucket(context.TODO(), dbi) | ||
if err != nil { | ||
return fmt.Errorf("could not delete bucket %s - %v", name, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *SDK) BucketLocationConstraintForRegion(region string) types.BucketLocationConstraint { | ||
for _, blc := range types.BucketLocationConstraint.Values() { | ||
if blc == region { | ||
return blc | ||
} | ||
} | ||
return "" | ||
} |