s3-share/internal/s3/minio.go
2022-05-10 12:49:34 +02:00

70 lines
1.5 KiB
Go

package s3
import (
"context"
"git.kapelle.org/niklas/s3share/internal/types"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/sirupsen/logrus"
)
type minioClient struct {
client *minio.Client
bucket string
}
func NewMinio(endpoint, bucket, accessKey, secretAccessKey string, ssl bool) (S3, error) {
logrus.Info("Creating minio client")
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretAccessKey, ""),
Secure: ssl,
})
if err != nil {
return nil, err
}
return &minioClient{
client: client,
bucket: bucket,
}, nil
}
func (m *minioClient) GetObject(ctx context.Context, key string) (ObjectReader, error) {
object, err := m.client.GetObject(ctx, m.bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
return object, nil
}
func (m *minioClient) KeyExists(ctx context.Context, key string) (bool, error) {
_, err := m.client.StatObject(ctx, m.bucket, key, minio.GetObjectOptions{})
if err != nil {
errResponse := minio.ToErrorResponse(err)
if errResponse.Code == "NoSuchKey" {
return false, nil
}
return false, err
}
return true, nil
}
func (m *minioClient) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) {
object, err := m.client.StatObject(ctx, m.bucket, key, minio.StatObjectOptions{})
if err != nil {
return nil, err
}
return &types.Metadata{
Size: object.Size,
ContentType: object.ContentType,
}, nil
}