Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joremysh committed Oct 28, 2024
1 parent d7daf7e commit 78d0d3c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
5 changes: 5 additions & 0 deletions minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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...")

Expand Down
90 changes: 67 additions & 23 deletions minio/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"encoding/json"
"testing"

"github.com/gofrs/uuid"
"github.com/minio/minio-go/v7"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

miniox "github.com/instill-ai/x/minio"
)

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()
Expand All @@ -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))
}

0 comments on commit 78d0d3c

Please sign in to comment.