-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
47 lines (40 loc) · 1.32 KB
/
main.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package main
import (
"context"
"encoding/json"
"flag"
"log"
"github.com/aws/aws-sdk-go-v2/config"
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
"namespacelabs.dev/foundation/universe/aws/s3"
)
var (
awsCredentialsFile = flag.String("aws_credentials_file", "", "Path to the AWS credentials file.")
)
func main() {
flag.Parse()
if *awsCredentialsFile == "" {
log.Fatalf("aws_credentials_file must be set")
}
ctx := context.Background()
// BucketConfigs are passed as additional arguments without flag names.
for _, jsonBucketConfig := range flag.Args() {
bc := &s3.BucketConfig{}
if err := json.Unmarshal([]byte(jsonBucketConfig), bc); err != nil {
log.Fatalf("Failed to unmarshal bucket config with error: %s", err)
}
awsCfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(bc.Region),
config.WithSharedCredentialsFiles([]string{*awsCredentialsFile}))
if err != nil {
log.Fatalf("Failed to create s3 client with: %s", err)
}
s3client := awss3.NewFromConfig(awsCfg)
if err = s3.EnsureBucketExists(ctx, s3client, bc); err != nil {
log.Fatalf("Failed to create s3 bucket with: %s", err)
}
}
}