/
s3.go
74 lines (64 loc) · 1.75 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
package storage
import (
"bytes"
"context"
"github.com/aws/aws-sdk-go-v2/aws"
awsCfg "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"
"github.com/edgetx/cloudbuild/config"
log "github.com/sirupsen/logrus"
)
type PutObjectToS3 interface {
PutObject(
ctx context.Context,
params *s3.PutObjectInput,
optFns ...func(*s3.Options),
) (*s3.PutObjectOutput, error)
}
type S3ArtifactStorage struct {
bucket string
s3 PutObjectToS3
}
func NewS3ArtifactStorage(bucket string, s3 PutObjectToS3) *S3ArtifactStorage {
return &S3ArtifactStorage{
bucket: bucket,
s3: s3,
}
}
func NewS3ArtifactStorageFromConfig(
ctx context.Context, c *config.CloudbuildOpts,
) *S3ArtifactStorage {
resolver := aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: c.StorageS3URL,
SigningRegion: region,
HostnameImmutable: c.StorageS3HostImmutable,
}, nil
})
cfg, err := awsCfg.LoadDefaultConfig(
ctx,
awsCfg.WithEndpointResolverWithOptions(resolver),
awsCfg.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
c.StorageS3AccessKey, c.StorageS3SecretKey, "",
),
),
)
if err != nil {
log.Fatalf("failed to load s3 config: %s", err)
}
s3Client := s3.NewFromConfig(cfg)
return NewS3ArtifactStorage(c.StorageS3Bucket, s3Client)
}
func (storage *S3ArtifactStorage) Upload(ctx context.Context, data []byte, fileName string) error {
input := &s3.PutObjectInput{
Bucket: aws.String(storage.bucket),
Key: aws.String(fileName),
Body: bytes.NewReader(data),
ACL: "public-read",
}
_, err := storage.s3.PutObject(ctx, input)
return err
}