Skip to content

Commit

Permalink
chore(minio): rename import to recognize external & internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
joremysh committed Sep 13, 2024
1 parent 579a69c commit 5fd9b35
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
37 changes: 33 additions & 4 deletions pkg/handler/mock_service_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions pkg/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/lifecycle"
"go.uber.org/zap"

miniogo "github.com/minio/minio-go/v7"

"github.com/instill-ai/model-backend/config"

log "github.com/instill-ai/model-backend/pkg/logger"
)

type MinioI interface {
UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error)
UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error)
UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error)
UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error)
DeleteFile(ctx context.Context, filePath string) (err error)
GetFile(ctx context.Context, filePath string) ([]byte, error)
GetFilesByPaths(ctx context.Context, filePaths []string) ([]FileContent, error)
Expand All @@ -32,7 +33,7 @@ type MinioI interface {
const Location = "us-east-1"

type Minio struct {
client *minio.Client
client *miniogo.Client
bucket string
}

Expand All @@ -44,7 +45,7 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
logger.Info("Initializing Minio client and bucket...")

endpoint := net.JoinHostPort(cfg.Host, cfg.Port)
client, err := minio.New(endpoint, &minio.Options{
client, err := miniogo.New(endpoint, &miniogo.Options{
Creds: credentials.NewStaticV4(cfg.RootUser, cfg.RootPwd, ""),
Secure: cfg.Secure,
})
Expand All @@ -66,7 +67,7 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
return &Minio{client: client, bucket: cfg.BucketName}, nil
}

if err = client.MakeBucket(ctx, cfg.BucketName, minio.MakeBucketOptions{
if err = client.MakeBucket(ctx, cfg.BucketName, miniogo.MakeBucketOptions{
Region: Location,
}); err != nil {
logger.Error("creating Bucket failed", zap.Error(err))
Expand All @@ -93,12 +94,12 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
return &Minio{client: client, bucket: cfg.BucketName}, nil
}

func (m *Minio) UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error) {
func (m *Minio) UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error) {
jsonData, _ := json.Marshal(fileContent)
return m.UploadFileBytes(ctx, filePath, jsonData, fileMimeType)
}

func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error) {
func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error) {
logger, err := log.GetZapLogger(ctx)
if err != nil {
return "", nil, err
Expand All @@ -107,14 +108,14 @@ func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes
reader := bytes.NewReader(fileBytes)

// Create the file path with folder structure
_, err = m.client.PutObject(ctx, m.bucket, filePath, reader, int64(len(fileBytes)), minio.PutObjectOptions{ContentType: fileMimeType})
_, err = m.client.PutObject(ctx, m.bucket, filePath, reader, int64(len(fileBytes)), miniogo.PutObjectOptions{ContentType: fileMimeType})
if err != nil {
logger.Error("Failed to upload file to MinIO", zap.Error(err))
return "", nil, err
}

// Get the object stat (metadata)
stat, err := m.client.StatObject(ctx, m.bucket, filePath, minio.StatObjectOptions{})
stat, err := m.client.StatObject(ctx, m.bucket, filePath, miniogo.StatObjectOptions{})
if err != nil {
return "", nil, err
}
Expand All @@ -135,7 +136,7 @@ func (m *Minio) DeleteFile(ctx context.Context, filePathName string) (err error)
return err
}
// Delete the file from MinIO
err = m.client.RemoveObject(ctx, m.bucket, filePathName, minio.RemoveObjectOptions{})
err = m.client.RemoveObject(ctx, m.bucket, filePathName, miniogo.RemoveObjectOptions{})
if err != nil {
logger.Error("Failed to delete file from MinIO", zap.Error(err))
return err
Expand All @@ -150,7 +151,7 @@ func (m *Minio) GetFile(ctx context.Context, filePathName string) ([]byte, error
}

// Get the object using the client
object, err := m.client.GetObject(ctx, m.bucket, filePathName, minio.GetObjectOptions{})
object, err := m.client.GetObject(ctx, m.bucket, filePathName, miniogo.GetObjectOptions{})
if err != nil {
logger.Error("Failed to get file from MinIO", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -192,7 +193,7 @@ func (m *Minio) GetFilesByPaths(ctx context.Context, filePaths []string) ([]File
go func(filePath string) {
defer wg.Done()

obj, err := m.client.GetObject(ctx, m.bucket, filePath, minio.GetObjectOptions{})
obj, err := m.client.GetObject(ctx, m.bucket, filePath, miniogo.GetObjectOptions{})
if err != nil {
logger.Error("Failed to get object from MinIO", zap.String("path", filePath), zap.Error(err))
errCh <- err
Expand Down
26 changes: 13 additions & 13 deletions pkg/mock/minio_i_mock.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/worker/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/minio/minio-go/v7"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"go.temporal.io/sdk/temporal"
Expand All @@ -16,6 +15,8 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/guregu/null.v4"

miniogo "github.com/minio/minio-go/v7"

"github.com/instill-ai/model-backend/config"
"github.com/instill-ai/model-backend/pkg/constant"
"github.com/instill-ai/model-backend/pkg/datamodel"
Expand Down Expand Up @@ -331,7 +332,7 @@ type UploadToMinioActivityRequest struct {

type UploadToMinioActivityResponse struct {
URL string
ObjectInfo *minio.ObjectInfo
ObjectInfo *miniogo.ObjectInfo
}

func (w *worker) UploadToMinioActivity(ctx context.Context, param *UploadToMinioActivityRequest) (*UploadToMinioActivityResponse, error) {
Expand Down

0 comments on commit 5fd9b35

Please sign in to comment.