Skip to content

Commit

Permalink
create and uplaod s3 functions
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Pagotto <pagottoo@gmail.com>
  • Loading branch information
pagottoo committed Jul 19, 2022
1 parent ca023e0 commit 8100c96
Showing 1 changed file with 73 additions and 8 deletions.
81 changes: 73 additions & 8 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package aws
import (
"context"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
Expand All @@ -11,15 +18,10 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/cip8/autoname"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)

func BucketRand(dryRun bool, trackers map[string]*pkg.ActionTracker) {
Expand Down Expand Up @@ -61,15 +63,15 @@ func BucketRand(dryRun bool, trackers map[string]*pkg.ActionTracker) {
LocationConstraint: aws.String(regionName),
},
})
}
}
if err != nil {
log.Println("failed to create bucket "+bucketName, err.Error())
os.Exit(1)
}
vc := &s3.VersioningConfiguration{}
vc.Status = aws.String(s3.BucketVersioningStatusEnabled)
versionConfigInput := &s3.PutBucketVersioningInput{
Bucket: aws.String(bucketName),
Bucket: aws.String(bucketName),
VersioningConfiguration: vc,
}
log.Printf("[DEBUG] S3 put bucket versioning: %#v", versionConfigInput)
Expand Down Expand Up @@ -338,3 +340,66 @@ func DestroyBucketsInUse(destroyBuckets bool) {
log.Println("Skip: DestroyBucketsInUse")
}
}

func CreateBucket(dryRun bool, name string) {
log.Println("createBucketCalled")

s3Client := s3.New(GetAWSSession())

log.Println("creating", "bucket", name)

regionName := viper.GetString("aws.region")
log.Println("region is ", regionName)
if !dryRun {
_, err := s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: &name,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(regionName),
},
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case s3.ErrCodeBucketAlreadyExists:
log.Println("Bucket already exists " + name)
os.Exit(1)
case s3.ErrCodeBucketAlreadyOwnedByYou:
log.Println("Bucket already exists but OwnedByYou, the process will continue: " + name)
}
} else {
log.Println("failed to create bucket "+name, err.Error())
os.Exit(1)
}
}
} else {
log.Printf("[#99] Dry-run mode, bucket creation skipped: %s", name)
}
viper.Set(fmt.Sprintf("bucket.%s.created", name), true)
viper.Set(fmt.Sprintf("bucket.%s.name", name), name)
viper.WriteConfig()
}

func UploadFile(bucket, key, fileName string) error {
// The session the S3 Uploader will use
//sess := session.Must(session.NewSession())

// Create an uploader with the session and default options
uploader := s3manager.NewUploader(GetAWSSession())

f, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("failed to open file %q, %v", fileName, err)
}

// Upload the file to S3.
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: f,
})
if err != nil {
return fmt.Errorf("failed to upload file, %v", err)
}
fmt.Printf("file uploaded to, %s\n", result.Location)
return nil
}

0 comments on commit 8100c96

Please sign in to comment.