Skip to content

Commit

Permalink
feat: base minio client
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jan 13, 2024
1 parent 82a9056 commit 17382ae
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion client/bucket/bucket.client.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 17382ae

Please sign in to comment.