/
s3_v2.go
124 lines (111 loc) · 3.97 KB
/
s3_v2.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package storage
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
s3api "github.com/aws/aws-sdk-go-v2/service/s3"
"io"
"os"
"time"
)
// s3v2 implements Storage using the Amazon Web Services - Simple Storage Service (S3).
// It uses the second version of the SDK.
//
// Reference: https://aws.amazon.com/s3/
// API: https://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html
// SDK: https://github.com/aws/aws-sdk-go-v2
type s3v2 struct {
client *s3api.Client
presign *s3api.PresignClient
bucket string
duration time.Duration
}
// UploadZip uploads a zip file of the given resource to S3.
//
// Resources can have a compressed representation of the resource itself that acts like a cache, it contains all the
// files from the said resource. This function uploads that zip file.
func (s *s3v2) UploadZip(ctx context.Context, resource Resource, file *os.File) error {
return UploadZip(ctx, resource, file, uploadFileS3v2(s.client, s.bucket, nil))
}
// UploadDir uploads the entire src directory to S3.
func (s *s3v2) UploadDir(ctx context.Context, resource Resource, src string) error {
return UploadDir(ctx, resource, src, uploadFileS3v2(s.client, s.bucket, resource))
}
// Download downloads a zip file of the given resource from S3.
func (s *s3v2) Download(ctx context.Context, resource Resource) (string, error) {
if err := validateResource(resource); err != nil {
return "", err
}
path := getZipLocation("", resource)
_, err := s.client.HeadObject(ctx, &s3api.HeadObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
})
if err != nil {
return "", err
}
out, err := s.presign.PresignGetObject(ctx, &s3api.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}, s3api.WithPresignExpires(s.duration))
if err != nil {
return "", err
}
return out.URL, nil
}
// GetFile returns the content of a file from the given path.
func (s *s3v2) GetFile(ctx context.Context, resource Resource, path string) ([]byte, error) {
return ReadFile(ctx, resource, path, readFileS3v2(s.client, s.bucket))
}
// NewS3v2 initializes a new implementation of Storage using the AWS S3 service.
func NewS3v2(client *s3api.Client, bucket string) Storage {
return &s3v2{
client: client,
presign: s3api.NewPresignClient(client),
bucket: bucket,
duration: 5 * time.Minute,
}
}
// readFileS3v2 generates a function that contains the interaction with S3 to read the content of a file.
func readFileS3v2(client *s3api.Client, bucket string) ReadFileFunc {
return func(ctx context.Context, resource Resource, path string) (io.ReadCloser, error) {
out, err := client.GetObject(ctx, &s3api.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(getLocation("", resource, path)),
})
if err != nil {
return nil, err
}
return out.Body, nil
}
}
// uploadFileS3v2 generates a function that allows to upload a single file in a path.
func uploadFileS3v2(client *s3api.Client, bucket string, resource Resource) WalkDirFunc {
return func(ctx context.Context, path string, body io.Reader) error {
// If Resource is nil, it will use the given path as-is, otherwise it will use the given path as a relative path
// to the given Resource.
if resource != nil {
path = getLocation("", resource, path)
}
_, err := client.PutObject(ctx, &s3api.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
Body: body,
})
return err
}
}
// deleteFileS3v2 generates a function that allows to delete a single file in a path.
func deleteFileS3v2(client *s3api.Client, bucket string, resource Resource) WalkDirFunc {
return func(ctx context.Context, path string, _ io.Reader) error {
// If Resource is nil, it will use the given path as-is, otherwise it will use the given path as a relative path
// to the given Resource.
if resource != nil {
path = getLocation("", resource, path)
}
_, err := client.DeleteObject(ctx, &s3api.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
})
return err
}
}