-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
99 lines (87 loc) · 2.91 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
98
99
package main
import (
"context"
"crypto/tls"
"net/http"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
log "github.com/sirupsen/logrus"
)
type MetadataBackend struct {
Client *s3.Client
Bucket string
}
type DeploymentBackend struct {
Client *s3.Client
Bucket string
}
func connectMetadatas3(mConf MetadataS3Config) *MetadataBackend {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("auto"),
config.WithHTTPClient(&http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(mConf.AccessKey, mConf.SecretKey, "")),
config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{URL: mConf.URL}, nil
})),
)
if err != nil {
log.Fatalf("Error while setting up s3 config: %v\n ", err)
}
client := s3.NewFromConfig(cfg)
metadata_client := &MetadataBackend{
Client: client,
Bucket: mConf.Bucket,
}
resp, err := metadata_client.Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(metadata_client.Bucket),
})
if err != nil {
log.Fatalf("Error while connecting to the metadata bucket %v\n ", err)
} else {
log.Infoln("Connection established to metadata bucket", metadata_client.Bucket)
}
// Abort if 0 metadata files found in bucket
numberOfFiles := 0
for _, obj := range resp.Contents {
if obj.Key != nil {
numberOfFiles++
}
}
if numberOfFiles == 0 {
log.Fatal("No Metadata files found in bucket. Length of files ", numberOfFiles)
}
return metadata_client
}
func connectDeployments3(dConf DeployS3Config) *DeploymentBackend {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("auto"),
config.WithHTTPClient(&http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(dConf.AccessKey, dConf.SecretKey, "")),
config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{URL: dConf.URL}, nil
})),
)
if err != nil {
log.Fatalf("Error while setting up s3 config: %v\n ", err)
}
client := s3.NewFromConfig(cfg)
deployment_client := &DeploymentBackend{
Client: client,
Bucket: dConf.Bucket,
}
_, err = deployment_client.Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(deployment_client.Bucket),
})
if err != nil {
log.Fatalf("Error while connecting to the deplpyment bucket %v\n ", err)
} else {
log.Infoln("Connection established to deployment bucket", deployment_client.Bucket)
}
return deployment_client
}