From 17382ae094c1a34f9991c4aca2040929c83bfec5 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 23:43:55 +0700 Subject: [PATCH] feat: base minio client --- client/bucket/bucket.client.go | 43 +++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/client/bucket/bucket.client.go b/client/bucket/bucket.client.go index 3f3b256..64ae646 100644 --- a/client/bucket/bucket.client.go +++ b/client/bucket/bucket.client.go @@ -1,8 +1,14 @@ package bucket import ( + "bytes" + "context" + "time" + "github.com/isd-sgcu/johnjud-file/cfgldr" "github.com/minio/minio-go/v7" + "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) type Client struct { @@ -17,10 +23,45 @@ func NewClient(conf cfgldr.Bucket, minioClient *minio.Client) *Client { } func (c *Client) Upload(file []byte, objectKey string) (string, string, error) { - return "", "", nil + ctx := context.Background() + ctx, cancel := context.WithTimeout(ctx, 50*time.Second) + defer cancel() + + buffer := bytes.NewReader(file) + + uploadOutput, err := c.minio.PutObject(context.Background(), c.conf.BucketName, objectKey, buffer, + buffer.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"}) + if err != nil { + log.Error(). + Err(err). + Str("service", "file"). + Str("module", "bucket client"). + Msgf("Couldn't upload object to %v:%v.", c.conf.BucketName, objectKey) + + return "", "", errors.Wrap(err, "Error while uploading the object") + } + + return uploadOutput.Location, uploadOutput.Key, nil } func (c *Client) Delete(objectKey string) error { + ctx := context.Background() + ctx, cancel := context.WithTimeout(ctx, 50*time.Second) + defer cancel() + + opts := minio.RemoveObjectOptions{ + GovernanceBypass: true, + } + err := c.minio.RemoveObject(context.Background(), c.conf.BucketName, objectKey, opts) + if err != nil { + log.Error(). + Err(err). + Str("service", "file"). + Str("module", "bucket client"). + Msgf("Couldn't delete object from bucket %v:%v.", c.conf.BucketName, objectKey) + + return errors.Wrap(err, "Error while deleting the object") + } return nil }