-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
s3.go
45 lines (38 loc) · 1.01 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
package s3browser
import (
"io"
"net/http"
"strings"
"github.com/minio/minio-go/v6"
)
type S3Client struct {
s3 *minio.Client
bucket string
}
func NewS3Client(endpoint, key, secret string, secure bool, bucket string) (S3Client, error) {
minioClient, err := minio.New(endpoint, key, secret, secure)
c := S3Client{
s3: minioClient,
bucket: bucket,
}
return c, err
}
func (c *S3Client) ForEachObject(fn func(minio.ObjectInfo)) error {
doneCh := make(chan struct{})
defer close(doneCh)
objectCh := c.s3.ListObjectsV2(c.bucket, "", true, doneCh)
for object := range objectCh {
if object.Err != nil {
return object.Err
}
fn(object)
}
return nil
}
func (c *S3Client) GetObject(filePath string, rangeHdr string) (io.ReadCloser, minio.ObjectInfo, http.Header, error) {
filePath = strings.TrimLeft(filePath, "/")
objectOptions := minio.GetObjectOptions{}
objectOptions.Set("Range", rangeHdr)
coreClient := minio.Core{Client: c.s3}
return coreClient.GetObject(c.bucket, filePath, objectOptions)
}