@@ -6,6 +6,7 @@ package storage
6
6
import (
7
7
"context"
8
8
"crypto/tls"
9
+ "fmt"
9
10
"io"
10
11
"net/http"
11
12
"net/url"
@@ -53,10 +54,12 @@ type MinioStorageConfig struct {
53
54
BasePath string `ini:"MINIO_BASE_PATH"`
54
55
UseSSL bool `ini:"MINIO_USE_SSL"`
55
56
InsecureSkipVerify bool `ini:"MINIO_INSECURE_SKIP_VERIFY"`
57
+ ChecksumAlgorithm string `ini:"MINIO_CHECKSUM_ALGORITHM"`
56
58
}
57
59
58
60
// MinioStorage returns a minio bucket storage
59
61
type MinioStorage struct {
62
+ cfg * MinioStorageConfig
60
63
ctx context.Context
61
64
client * minio.Client
62
65
bucket string
@@ -91,6 +94,10 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
91
94
}
92
95
config := configInterface .(MinioStorageConfig )
93
96
97
+ if config .ChecksumAlgorithm != "" && config .ChecksumAlgorithm != "default" && config .ChecksumAlgorithm != "md5" {
98
+ return nil , fmt .Errorf ("invalid minio checksum algorithm: %s" , config .ChecksumAlgorithm )
99
+ }
100
+
94
101
log .Info ("Creating Minio storage at %s:%s with base path %s" , config .Endpoint , config .Bucket , config .BasePath )
95
102
96
103
minioClient , err := minio .New (config .Endpoint , & minio.Options {
@@ -113,6 +120,7 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
113
120
}
114
121
115
122
return & MinioStorage {
123
+ cfg : & config ,
116
124
ctx : ctx ,
117
125
client : minioClient ,
118
126
bucket : config .Bucket ,
@@ -124,7 +132,7 @@ func (m *MinioStorage) buildMinioPath(p string) string {
124
132
return util .PathJoinRelX (m .basePath , p )
125
133
}
126
134
127
- // Open open a file
135
+ // Open opens a file
128
136
func (m * MinioStorage ) Open (path string ) (Object , error ) {
129
137
opts := minio.GetObjectOptions {}
130
138
object , err := m .client .GetObject (m .ctx , m .bucket , m .buildMinioPath (path ), opts )
@@ -134,15 +142,22 @@ func (m *MinioStorage) Open(path string) (Object, error) {
134
142
return & minioObject {object }, nil
135
143
}
136
144
137
- // Save save a file to minio
145
+ // Save saves a file to minio
138
146
func (m * MinioStorage ) Save (path string , r io.Reader , size int64 ) (int64 , error ) {
139
147
uploadInfo , err := m .client .PutObject (
140
148
m .ctx ,
141
149
m .bucket ,
142
150
m .buildMinioPath (path ),
143
151
r ,
144
152
size ,
145
- minio.PutObjectOptions {ContentType : "application/octet-stream" },
153
+ minio.PutObjectOptions {
154
+ ContentType : "application/octet-stream" ,
155
+ // some storages like:
156
+ // * https://developers.cloudflare.com/r2/api/s3/api/
157
+ // * https://www.backblaze.com/b2/docs/s3_compatible_api.html
158
+ // do not support "x-amz-checksum-algorithm" header, so use legacy MD5 checksum
159
+ SendContentMd5 : m .cfg .ChecksumAlgorithm == "md5" ,
160
+ },
146
161
)
147
162
if err != nil {
148
163
return 0 , convertMinioErr (err )
0 commit comments