From 492fa697573c344bf17b81235f2e55d24da881ec Mon Sep 17 00:00:00 2001 From: Jeremy Shih Date: Mon, 28 Oct 2024 15:13:28 +0800 Subject: [PATCH] wip --- minio/minio.go | 5 +++ minio/minio_test.go | 90 +++++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/minio/minio.go b/minio/minio.go index 5a29cc8..ed11cb7 100644 --- a/minio/minio.go +++ b/minio/minio.go @@ -24,6 +24,7 @@ type MinioI interface { DeleteFile(ctx context.Context, logger *zap.Logger, filePath string) (err error) GetFile(ctx context.Context, logger *zap.Logger, filePath string) ([]byte, error) GetFilesByPaths(ctx context.Context, logger *zap.Logger, filePaths []string) ([]FileContent, error) + GetClient() *miniogo.Client } type ExpiryRule struct { @@ -44,6 +45,10 @@ type minio struct { expiryRuleConfig map[string]int } +func (m *minio) GetClient() *miniogo.Client { + return m.client +} + func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.Logger, expiryRules ...ExpiryRule) (MinioI, error) { logger.Info("Initializing Minio client and bucket...") diff --git a/minio/minio_test.go b/minio/minio_test.go index c706215..f286c29 100644 --- a/minio/minio_test.go +++ b/minio/minio_test.go @@ -5,7 +5,7 @@ import ( "encoding/json" "testing" - "github.com/gofrs/uuid" + "github.com/minio/minio-go/v7" "github.com/stretchr/testify/require" "go.uber.org/zap" @@ -13,7 +13,7 @@ import ( ) func TestMinio(t *testing.T) { - t.Skipf("only for testing on local") + // t.Skipf("only for testing on local") ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -25,34 +25,78 @@ func TestMinio(t *testing.T) { RootUser: "minioadmin", RootPwd: "minioadmin", BucketName: "instill-ai-model", - }, log) + }, log, miniox.ExpiryRule{ + Tag: "unit-test", + ExpirationDays: 1, + }) require.NoError(t, err) - t.Log("test upload file to minio") - fileName, _ := uuid.NewV4() - uid, _ := uuid.NewV4() + // t.Log("test upload file to minio") + // fileName, _ := uuid.NewV4() + // uid, _ := uuid.NewV4() + // + // data := make(map[string]string) + // data["uid"] = uid.String() + // jsonBytes, _ := json.Marshal(data) - data := make(map[string]string) - data["uid"] = uid.String() - jsonBytes, _ := json.Marshal(data) + // url, stat, err := mc.UploadFile(ctx, log, &miniox.UploadFileParam{ + // FilePath: fileName.String(), + // FileContent: data, + // FileMimeType: "application/json", + // ExpiryRuleTag: "unit-test", + // }) + // require.NoError(t, err) + // t.Log("url:", url) + // t.Log("size:", stat.Size) + // + // fileBytes, err := mc.GetFile(ctx, log, fileName.String()) + // require.NoError(t, err) + // require.Equal(t, jsonBytes, fileBytes) + // t.Log(string(fileBytes)) - url, stat, err := mc.UploadFile(ctx, log, &miniox.UploadFileParam{ - FilePath: fileName.String(), - FileContent: data, - FileMimeType: "application/json", - }) - require.NoError(t, err) - t.Log("url:", url) - t.Log("size:", stat.Size) + // err = mc.DeleteFile(ctx, log, fileName.String()) + // require.NoError(t, err) + // + fileBytes, err := mc.GetFile(ctx, log, "88573fd5-5a21-4f62-9633-997b3a5a10d5") + t.Log(string(fileBytes)) + require.Error(t, err) - fileBytes, err := mc.GetFile(ctx, log, fileName.String()) - require.NoError(t, err) - require.Equal(t, jsonBytes, fileBytes) + fileBytes, err = mc.GetFile(ctx, log, "12f76fb4-c7cb-4b79-bd3b-fc7b9e0759e0") + t.Log(string(fileBytes)) + require.Error(t, err) +} + +func TestMinio_GetClient(t *testing.T) { + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + log, _ := zap.NewDevelopment() + const bucketName = "instill-ai-model" + mc, err := miniox.NewMinioClientAndInitBucket(ctx, &miniox.Config{ + Host: "localhost", + Port: "19000", + RootUser: "minioadmin", + RootPwd: "minioadmin", + BucketName: bucketName, + }, log, miniox.ExpiryRule{ + Tag: "unit-test", + ExpirationDays: 1, + }) - err = mc.DeleteFile(ctx, log, fileName.String()) require.NoError(t, err) - _, err = mc.GetFile(ctx, log, fileName.String()) - require.Error(t, err) + c := mc.GetClient() + for objectInfo := range c.ListObjects(ctx, bucketName, minio.ListObjectsOptions{}) { + jsonBytes, _ := json.Marshal(objectInfo) + t.Log(string(jsonBytes)) + tags, err := c.GetObjectTagging(ctx, bucketName, objectInfo.Key, minio.GetObjectTaggingOptions{}) + require.NoError(t, err) + t.Log(*tags) + } + // cfg, err := c.GetBucketLifecycle(ctx, bucketName) + // require.NoError(t, err) + // jsonBytes, _ := json.Marshal(cfg) + // t.Log(string(jsonBytes)) }