forked from eduardolat/pgbackweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.go
More file actions
180 lines (158 loc) · 4.06 KB
/
s3.go
File metadata and controls
180 lines (158 loc) · 4.06 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package storage
import (
"context"
"fmt"
"io"
"time"
"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/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/eduardolat/pgbackweb/internal/util/strutil"
)
// createS3Client creates a new S3 client
func createS3Client(
accessKey, secretKey, region, endpoint string,
) (*s3.Client, error) {
credentialsProvider := credentials.NewStaticCredentialsProvider(
accessKey, secretKey, "",
)
//nolint:all
endpointResolver := aws.EndpointResolverFunc(func(
_ string, _ string,
) (aws.Endpoint, error) {
return aws.Endpoint{
HostnameImmutable: true,
URL: endpoint,
}, nil
})
//nolint:all
conf, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(region),
config.WithEndpointResolver(endpointResolver),
config.WithCredentialsProvider(credentialsProvider),
)
if err != nil {
return nil, fmt.Errorf("error initializing storage config: %w", err)
}
// Create S3 client with path-style addressing enabled
// This is required for S3-compatible services like Backblaze
s3Client := s3.NewFromConfig(conf, func(o *s3.Options) {
o.UsePathStyle = true
})
return s3Client, nil
}
// S3Test tests the connection to S3
func (Client) S3Test(
accessKey, secretKey, region, endpoint, bucketName string,
) error {
s3Client, err := createS3Client(
accessKey, secretKey, region, endpoint,
)
if err != nil {
return err
}
_, err = s3Client.HeadBucket(
context.TODO(),
&s3.HeadBucketInput{
Bucket: aws.String(bucketName),
},
)
if err != nil {
return fmt.Errorf("failed to test S3 bucket: %w", err)
}
return nil
}
// S3Upload uploads a file to S3 from a reader.
//
// Returns the file size, in bytes.
func (Client) S3Upload(
accessKey, secretKey, region, endpoint, bucketName, key string,
fileReader io.Reader,
) (int64, error) {
s3Client, err := createS3Client(
accessKey, secretKey, region, endpoint,
)
if err != nil {
return 0, err
}
key = strutil.RemoveLeadingSlash(key)
contentType := strutil.GetContentTypeFromFileName(key)
uploader := manager.NewUploader(s3Client)
_, err = uploader.Upload(
context.TODO(),
&s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
Body: fileReader,
ContentType: aws.String(contentType),
},
)
if err != nil {
return 0, fmt.Errorf("failed to upload file to S3: %w", err)
}
fileHead, err := s3Client.HeadObject(
context.TODO(),
&s3.HeadObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
},
)
if err != nil {
return 0, fmt.Errorf("failed to get uploaded file info from S3: %w", err)
}
var fileSize int64
if fileHead.ContentLength != nil {
fileSize = *fileHead.ContentLength
}
return fileSize, nil
}
// S3Delete deletes a file from S3
func (Client) S3Delete(
accessKey, secretKey, region, endpoint, bucketName, key string,
) error {
s3Client, err := createS3Client(
accessKey, secretKey, region, endpoint,
)
if err != nil {
return err
}
key = strutil.RemoveLeadingSlash(key)
_, err = s3Client.DeleteObject(
context.TODO(),
&s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
},
)
if err != nil {
return fmt.Errorf("failed to delete file from S3: %w", err)
}
return nil
}
// S3GetDownloadLink generates a presigned URL for downloading a file from S3
func (Client) S3GetDownloadLink(
accessKey, secretKey, region, endpoint, bucketName, key string,
expiration time.Duration,
) (string, error) {
s3Client, err := createS3Client(
accessKey, secretKey, region, endpoint,
)
if err != nil {
return "", fmt.Errorf("failed to create S3 client: %w", err)
}
presigned, err := s3.NewPresignClient(s3Client).PresignGetObject(
context.TODO(),
&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
},
s3.WithPresignExpires(expiration),
)
if err != nil {
return "", fmt.Errorf("failed to generate presigned URL: %w", err)
}
return presigned.URL, nil
}