2018-07-14 08:48:22 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2018-07-27 10:56:28 +00:00
|
|
|
"bytes"
|
2021-01-15 03:19:59 +00:00
|
|
|
"context"
|
2018-07-14 08:48:22 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-10-27 18:05:02 +00:00
|
|
|
"sync/atomic"
|
2018-07-14 08:48:22 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2021-01-15 03:19:59 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2018-07-14 08:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-03 10:40:58 +00:00
|
|
|
metadataName = ".metadata.json"
|
2018-07-14 08:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type s3Client struct {
|
2021-04-03 10:40:58 +00:00
|
|
|
Config *Config
|
|
|
|
minio *minio.Client
|
|
|
|
ctx context.Context
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
// Config holds values to configure the driver
|
|
|
|
type Config struct {
|
|
|
|
AccessKeyID string
|
|
|
|
SecretAccessKey string
|
|
|
|
Region string
|
|
|
|
Endpoint string
|
|
|
|
Mounter string
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:07:16 +00:00
|
|
|
type FSMeta struct {
|
|
|
|
BucketName string `json:"Name"`
|
|
|
|
Prefix string `json:"Prefix"`
|
|
|
|
Mounter string `json:"Mounter"`
|
2021-07-16 13:12:57 +00:00
|
|
|
MountOptions []string `json:"MountOptions"`
|
2021-04-05 13:07:16 +00:00
|
|
|
CapacityBytes int64 `json:"CapacityBytes"`
|
2018-07-27 10:56:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func NewClient(cfg *Config) (*s3Client, error) {
|
2018-07-14 08:48:22 +00:00
|
|
|
var client = &s3Client{}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
client.Config = cfg
|
|
|
|
u, err := url.Parse(client.Config.Endpoint)
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ssl := u.Scheme == "https"
|
|
|
|
endpoint := u.Hostname()
|
|
|
|
if u.Port() != "" {
|
|
|
|
endpoint = u.Hostname() + ":" + u.Port()
|
|
|
|
}
|
2021-01-15 03:19:59 +00:00
|
|
|
minioClient, err := minio.New(endpoint, &minio.Options{
|
2023-06-09 08:10:33 +00:00
|
|
|
Creds: credentials.NewStaticV4(client.Config.AccessKeyID, client.Config.SecretAccessKey, ""),
|
2021-01-15 03:19:59 +00:00
|
|
|
Secure: ssl,
|
|
|
|
})
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.minio = minioClient
|
2021-01-15 03:19:59 +00:00
|
|
|
client.ctx = context.Background()
|
2018-07-14 08:48:22 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func NewClientFromSecret(secret map[string]string) (*s3Client, error) {
|
|
|
|
return NewClient(&Config{
|
|
|
|
AccessKeyID: secret["accessKeyID"],
|
|
|
|
SecretAccessKey: secret["secretAccessKey"],
|
|
|
|
Region: secret["region"],
|
|
|
|
Endpoint: secret["endpoint"],
|
2019-03-10 11:19:02 +00:00
|
|
|
// Mounter is set in the volume preferences, not secrets
|
|
|
|
Mounter: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func (client *s3Client) BucketExists(bucketName string) (bool, error) {
|
2021-01-15 03:19:59 +00:00
|
|
|
return client.minio.BucketExists(client.ctx, bucketName)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func (client *s3Client) CreateBucket(bucketName string) error {
|
|
|
|
return client.minio.MakeBucket(client.ctx, bucketName, minio.MakeBucketOptions{Region: client.Config.Region})
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func (client *s3Client) CreatePrefix(bucketName string, prefix string) error {
|
2021-07-16 13:33:13 +00:00
|
|
|
if prefix != "" {
|
|
|
|
_, err := client.minio.PutObject(client.ctx, bucketName, prefix+"/", bytes.NewReader([]byte("")), 0, minio.PutObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-27 19:37:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
func (client *s3Client) RemovePrefix(bucketName string, prefix string) error {
|
2021-04-07 16:06:56 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = client.removeObjects(bucketName, prefix); err == nil {
|
|
|
|
return client.minio.RemoveObject(client.ctx, bucketName, prefix, minio.RemoveObjectOptions{})
|
2021-04-05 13:07:16 +00:00
|
|
|
}
|
2021-04-07 16:06:56 +00:00
|
|
|
|
2021-04-08 14:35:31 +00:00
|
|
|
glog.Warningf("removeObjects failed with: %s, will try removeObjectsOneByOne", err)
|
2021-04-07 16:06:56 +00:00
|
|
|
|
|
|
|
if err = client.removeObjectsOneByOne(bucketName, prefix); err == nil {
|
|
|
|
return client.minio.RemoveObject(client.ctx, bucketName, prefix, minio.RemoveObjectOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2021-04-03 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *s3Client) RemoveBucket(bucketName string) error {
|
2021-04-07 16:06:56 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = client.removeObjects(bucketName, ""); err == nil {
|
|
|
|
return client.minio.RemoveBucket(client.ctx, bucketName)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2021-04-07 16:06:56 +00:00
|
|
|
|
2021-04-08 14:35:31 +00:00
|
|
|
glog.Warningf("removeObjects failed with: %s, will try removeObjectsOneByOne", err)
|
2021-04-07 16:06:56 +00:00
|
|
|
|
|
|
|
if err = client.removeObjectsOneByOne(bucketName, ""); err == nil {
|
|
|
|
return client.minio.RemoveBucket(client.ctx, bucketName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:07:16 +00:00
|
|
|
func (client *s3Client) removeObjects(bucketName, prefix string) error {
|
2021-01-15 03:19:59 +00:00
|
|
|
objectsCh := make(chan minio.ObjectInfo)
|
2018-07-14 08:48:22 +00:00
|
|
|
var listErr error
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(objectsCh)
|
|
|
|
|
2021-01-15 03:19:59 +00:00
|
|
|
for object := range client.minio.ListObjects(
|
|
|
|
client.ctx,
|
|
|
|
bucketName,
|
2021-04-05 13:07:16 +00:00
|
|
|
minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
2018-07-14 08:48:22 +00:00
|
|
|
if object.Err != nil {
|
|
|
|
listErr = object.Err
|
|
|
|
return
|
|
|
|
}
|
2021-01-15 03:19:59 +00:00
|
|
|
objectsCh <- object
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if listErr != nil {
|
|
|
|
glog.Error("Error listing objects", listErr)
|
|
|
|
return listErr
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
default:
|
2021-01-15 03:19:59 +00:00
|
|
|
opts := minio.RemoveObjectsOptions{
|
|
|
|
GovernanceBypass: true,
|
|
|
|
}
|
|
|
|
errorCh := client.minio.RemoveObjects(client.ctx, bucketName, objectsCh, opts)
|
2021-04-07 16:06:56 +00:00
|
|
|
haveErrWhenRemoveObjects := false
|
2018-07-14 08:48:22 +00:00
|
|
|
for e := range errorCh {
|
2021-04-09 13:45:52 +00:00
|
|
|
glog.Errorf("Failed to remove object %s, error: %s", e.ObjectName, e.Err)
|
|
|
|
haveErrWhenRemoveObjects = true
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2021-04-07 16:06:56 +00:00
|
|
|
if haveErrWhenRemoveObjects {
|
2018-07-14 08:48:22 +00:00
|
|
|
return fmt.Errorf("Failed to remove all objects of bucket %s", bucketName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:40:58 +00:00
|
|
|
return nil
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2018-07-27 10:56:28 +00:00
|
|
|
|
2021-04-07 16:06:56 +00:00
|
|
|
// will delete files one by one without file lock
|
|
|
|
func (client *s3Client) removeObjectsOneByOne(bucketName, prefix string) error {
|
2021-07-26 21:04:17 +00:00
|
|
|
parallelism := 16
|
2023-10-27 18:47:25 +00:00
|
|
|
objectsCh := make(chan minio.ObjectInfo, parallelism)
|
2021-07-26 21:04:17 +00:00
|
|
|
guardCh := make(chan int, parallelism)
|
2021-04-07 16:06:56 +00:00
|
|
|
var listErr error
|
2023-10-27 18:05:02 +00:00
|
|
|
var totalObjects int64 = 0
|
|
|
|
var removeErrors int64 = 0
|
2021-04-07 16:06:56 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(objectsCh)
|
|
|
|
|
|
|
|
for object := range client.minio.ListObjects(client.ctx, bucketName,
|
|
|
|
minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
|
|
|
if object.Err != nil {
|
|
|
|
listErr = object.Err
|
|
|
|
return
|
|
|
|
}
|
2023-10-27 18:05:02 +00:00
|
|
|
atomic.AddInt64(&totalObjects, 1)
|
2021-04-07 16:06:56 +00:00
|
|
|
objectsCh <- object
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if listErr != nil {
|
|
|
|
glog.Error("Error listing objects", listErr)
|
|
|
|
return listErr
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:04:17 +00:00
|
|
|
for object := range objectsCh {
|
|
|
|
guardCh <- 1
|
2023-10-27 17:59:03 +00:00
|
|
|
go func(obj minio.ObjectInfo) {
|
|
|
|
err := client.minio.RemoveObject(client.ctx, bucketName, obj.Key,
|
|
|
|
minio.RemoveObjectOptions{VersionID: obj.VersionID})
|
2021-04-07 16:06:56 +00:00
|
|
|
if err != nil {
|
2023-10-27 17:59:03 +00:00
|
|
|
glog.Errorf("Failed to remove object %s, error: %s", obj.Key, err)
|
2023-10-27 18:05:02 +00:00
|
|
|
atomic.AddInt64(&removeErrors, 1)
|
2021-04-07 16:06:56 +00:00
|
|
|
}
|
2021-07-26 21:04:17 +00:00
|
|
|
<- guardCh
|
2023-10-27 17:59:03 +00:00
|
|
|
}(object)
|
2021-07-26 21:04:17 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < parallelism; i++ {
|
|
|
|
guardCh <- 1
|
2021-04-07 16:06:56 +00:00
|
|
|
}
|
2021-07-26 21:04:17 +00:00
|
|
|
for i := 0; i < parallelism; i++ {
|
|
|
|
<- guardCh
|
|
|
|
}
|
|
|
|
|
|
|
|
if removeErrors > 0 {
|
|
|
|
return fmt.Errorf("Failed to remove %v objects out of total %v of path %s", removeErrors, totalObjects, bucketName)
|
2021-04-07 16:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|