s3-share/internal/s3/minio.go

70 lines
1.5 KiB
Go
Raw Normal View History

2022-05-09 12:52:18 +00:00
package s3
import (
"context"
2022-05-10 10:49:34 +00:00
"git.kapelle.org/niklas/s3share/internal/types"
2022-05-09 12:52:18 +00:00
"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
}
2022-05-09 13:24:42 +00:00
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
}
2022-05-10 10:49:34 +00:00
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
}