/
s3.go
97 lines (80 loc) · 2.8 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package aws
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/kubefirst/runtime/pkg"
"github.com/rs/zerolog/log"
)
// CreateBucket
func (conf *AWSConfiguration) CreateBucket(bucketName string) (*s3.CreateBucketOutput, error) {
s3Client := s3.NewFromConfig(conf.Config)
log.Info().Msg(conf.Config.Region)
// Determine called region and whether or not it's a valid location
// constraint for S3
validLocationConstraints := s3Types.BucketLocationConstraint(conf.Config.Region)
var locationConstraint string
for _, location := range validLocationConstraints.Values() {
if string(location) == conf.Config.Region {
locationConstraint = conf.Config.Region
break
} else {
// It defaults to us-east-1 anyway
locationConstraint = "us-east-1"
}
}
// Create bucket
log.Info().Msgf("creating s3 bucket %s with location constraint %s", bucketName, locationConstraint)
s3CreateBucketInput := &s3.CreateBucketInput{}
s3CreateBucketInput.Bucket = aws.String(bucketName)
if conf.Config.Region != pkg.DefaultS3Region {
s3CreateBucketInput.CreateBucketConfiguration = &s3Types.CreateBucketConfiguration{
LocationConstraint: s3Types.BucketLocationConstraint(locationConstraint),
}
}
bucket, err := s3Client.CreateBucket(context.Background(), s3CreateBucketInput)
if err != nil {
return &s3.CreateBucketOutput{}, fmt.Errorf("error creating s3 bucket %s: %s", bucketName, err)
}
versionConfigInput := &s3.PutBucketVersioningInput{
Bucket: aws.String(bucketName),
VersioningConfiguration: &s3Types.VersioningConfiguration{
Status: s3Types.BucketVersioningStatusEnabled,
},
}
_, err = s3Client.PutBucketVersioning(context.Background(), versionConfigInput)
if err != nil {
return &s3.CreateBucketOutput{}, fmt.Errorf("error creating s3 bucket %s: %s", bucketName, err)
}
return bucket, nil
}
// DeleteBucket
func (conf *AWSConfiguration) DeleteBucket(bucketName string) error {
s3Client := s3.NewFromConfig(conf.Config)
// Create bucket
log.Info().Msgf("deleting s3 bucket %s", bucketName)
s3DeleteBucketInput := &s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
}
_, err := s3Client.DeleteBucket(context.Background(), s3DeleteBucketInput)
if err != nil {
return fmt.Errorf("error deleting s3 bucket %s: %s", bucketName, err)
}
return nil
}
func (conf *AWSConfiguration) ListBuckets() (*s3.ListBucketsOutput, error) {
fmt.Println("listing buckets")
s3Client := s3.NewFromConfig(conf.Config)
buckets, err := s3Client.ListBuckets(context.Background(), &s3.ListBucketsInput{})
if err != nil {
return &s3.ListBucketsOutput{}, err
}
return buckets, nil
}