generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstorage.go
173 lines (147 loc) · 4.7 KB
/
storage.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
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
package main
import (
"bytes"
"compress/gzip"
"context"
"fmt"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"io"
"os"
)
type StorageClient struct {
minioClient *minio.Client
bucket string
}
func NewStorageClient() (*StorageClient, error) {
endpoint := os.Getenv("R2_ENDPOINT")
if endpoint == "" {
return nil, fmt.Errorf("R2_ENDPOINT environment variable not set")
}
accessKeyID := os.Getenv("R2_ACCESS_KEY_ID")
if accessKeyID == "" {
return nil, fmt.Errorf("R2_ACCESS_KEY_ID environment variable not set")
}
secretAccessKey := os.Getenv("R2_SECRET_ACCESS_KEY")
if secretAccessKey == "" {
return nil, fmt.Errorf("R2_SECRET_ACCESS_KEY environment variable not set")
}
useSSL := os.Getenv("R2_USE_SSL")
if useSSL == "" {
return nil, fmt.Errorf("R2_USE_SSL environment variable not set")
}
bucket := os.Getenv("R2_BUCKET")
if bucket == "" {
return nil, fmt.Errorf("R2_BUCKET environment variable not set")
}
// Convert useSSL to boolean
var ssl bool
if useSSL == "true" || useSSL == "1" {
ssl = true
} else {
ssl = false
}
// Remove "https://" or "http://" prefix from endpoint if present
endpoint = trimEndpointScheme(endpoint)
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: ssl,
Region: "auto", // For Cloudflare R2
})
if err != nil {
return nil, fmt.Errorf("failed to create MinIO client: %v", err)
}
return &StorageClient{minioClient: minioClient, bucket: bucket}, nil
}
// UploadFile uploads a file with the specified content type and optional compression
func (sc *StorageClient) UploadFile(ctx context.Context, objectName string, content []byte, contentType string, compress bool) error {
var reader *bytes.Reader
var size int64
if compress {
// Compress the content using gzip
var compressedContent bytes.Buffer
gzipWriter := gzip.NewWriter(&compressedContent)
_, err := gzipWriter.Write(content)
if err != nil {
return fmt.Errorf("failed to compress content: %v", err)
}
gzipWriter.Close() // Make sure to close the writer to flush the data
reader = bytes.NewReader(compressedContent.Bytes())
size = int64(compressedContent.Len())
} else {
reader = bytes.NewReader(content)
size = int64(len(content))
}
// Set appropriate options
opts := minio.PutObjectOptions{
ContentType: contentType,
}
if compress {
opts.ContentEncoding = "gzip"
}
// Upload the content
_, err := sc.minioClient.PutObject(ctx, sc.bucket, objectName, reader, size, opts)
if err != nil {
return fmt.Errorf("failed to upload object %s: %v", objectName, err)
}
return nil
}
// DownloadFile downloads a file from storage and returns its content
func (sc *StorageClient) DownloadFile(ctx context.Context, objectName string) ([]byte, error) {
// Get the object from storage
object, err := sc.minioClient.GetObject(ctx, sc.bucket, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get object %s: %v", objectName, err)
}
defer object.Close()
// Read the object content
var buf bytes.Buffer
_, err = buf.ReadFrom(object)
if err != nil {
return nil, fmt.Errorf("failed to read object %s: %v", objectName, err)
}
// Check if the content is compressed
info, err := object.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stat object %s: %v", objectName, err)
}
content := buf.Bytes()
if info.Metadata.Get("Content-Encoding") == "gzip" {
// Decompress the content
gzipReader, err := gzip.NewReader(bytes.NewReader(content))
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader for object %s: %v", objectName, err)
}
defer gzipReader.Close()
decompressedContent, err := io.ReadAll(gzipReader)
if err != nil {
return nil, fmt.Errorf("failed to decompress object %s: %v", objectName, err)
}
content = decompressedContent
}
return content, nil
}
func (sc *StorageClient) FileExists(ctx context.Context, objectName string) (bool, error) {
// Attempt to get object information
_, err := sc.minioClient.StatObject(ctx, sc.bucket, objectName, minio.StatObjectOptions{})
if err != nil {
// If the error is because the object does not exist, return false
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
return false, nil
}
// Otherwise, return the error
return false, fmt.Errorf("error checking if object %s exists: %v", objectName, err)
}
// If no error, the object exists
return true, nil
}
// Helper function to trim scheme from endpoint
func trimEndpointScheme(endpoint string) string {
if len(endpoint) >= 8 && endpoint[:8] == "https://" {
return endpoint[8:]
}
if len(endpoint) >= 7 && endpoint[:7] == "http://" {
return endpoint[7:]
}
return endpoint
}