12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package storage
- import (
- "context"
- "io"
- "git.linuxforward.com/byom/byom-golang-lib/pkg/errors"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
- )
- type MinioConfig struct {
- Endpoint string
- AccessKeyID string
- SecretAccessKey string
- UseSSL bool
- BucketName string
- }
- type MinioClient struct {
- client *minio.Client
- bucketName string
- }
- func NewMinioClient(config MinioConfig) (*MinioClient, error) {
- if config.Endpoint == "" {
- return nil, errors.NewStorageError("initialize", "", errors.ErrInvalidInput)
- }
- client, err := minio.New(config.Endpoint, &minio.Options{
- Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
- Secure: config.UseSSL,
- })
- if err != nil {
- return nil, errors.NewStorageError("create_client", "", err)
- }
- // Ensure bucket exists
- ctx := context.Background()
- exists, err := client.BucketExists(ctx, config.BucketName)
- if err != nil {
- return nil, errors.NewStorageError("check_bucket", config.BucketName, err)
- }
- if !exists {
- err = client.MakeBucket(ctx, config.BucketName, minio.MakeBucketOptions{})
- if err != nil {
- return nil, errors.NewStorageError("create_bucket", config.BucketName, err)
- }
- }
- return &MinioClient{
- client: client,
- bucketName: config.BucketName,
- }, nil
- }
- // Client returns the underlying Minio client
- func (m *MinioClient) Client() *minio.Client {
- return m.client
- }
- // BucketName returns the name of the bucket
- func (m *MinioClient) BucketName() string {
- return m.bucketName
- }
- // PutObject is a helper method to upload an object to the bucket
- func (m *MinioClient) PutObject(ctx context.Context, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) {
- return m.client.PutObject(ctx, m.bucketName, objectName, reader, objectSize, opts)
- }
- // GetObject is a helper method to download an object from the bucket
- func (m *MinioClient) GetObject(ctx context.Context, objectName string, opts minio.GetObjectOptions) (*minio.Object, error) {
- return m.client.GetObject(ctx, m.bucketName, objectName, opts)
- }
|