From 25daf772a9972f33a24fb3a156b109f5b40478c6 Mon Sep 17 00:00:00 2001 From: Jeremy Shih Date: Fri, 25 Oct 2024 16:27:34 +0800 Subject: [PATCH] wip --- minio/minio.go | 84 ++++++++++++++++++++++++++++----------------- minio/minio_test.go | 7 ++++ 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/minio/minio.go b/minio/minio.go index a938b7b..c89beda 100644 --- a/minio/minio.go +++ b/minio/minio.go @@ -32,6 +32,11 @@ func (g ExpiryGroup) String() string { return string(g) } +type ExpiryRule struct { + Tag string + ExpirationDays int +} + const ( Location = "us-east-1" @@ -55,7 +60,7 @@ type minio struct { bucket string } -func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.Logger) (MinioI, error) { +func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.Logger, expiryRules ...ExpiryRule) (MinioI, error) { logger.Info("Initializing Minio client and bucket...") endpoint := net.JoinHostPort(cfg.Host, cfg.Port) @@ -98,45 +103,60 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.L Days: lifecycle.ExpirationDays(30), }, }, - { - ID: "free-plan-rule", - Status: StatusEnabled, - Expiration: lifecycle.Expiration{ - Days: lifecycle.ExpirationDays(expiryTimeConfig[FreePlanExpiry]), - }, - RuleFilter: lifecycle.Filter{ - Tag: lifecycle.Tag{ - Key: ExpiryTag, - Value: FreePlanExpiry.String(), - }, - }, - }, - { - ID: "pro-plan-rule", - Status: StatusEnabled, - Expiration: lifecycle.Expiration{ - Days: lifecycle.ExpirationDays(expiryTimeConfig[ProPlanExpiry]), - }, - RuleFilter: lifecycle.Filter{ - Tag: lifecycle.Tag{ - Key: ExpiryTag, - Value: ProPlanExpiry.String(), - }, - }, - }, - { - ID: "team-plan-rule", + // { + // ID: "free-plan-rule", + // Status: StatusEnabled, + // Expiration: lifecycle.Expiration{ + // Days: lifecycle.ExpirationDays(expiryTimeConfig[FreePlanExpiry]), + // }, + // RuleFilter: lifecycle.Filter{ + // Tag: lifecycle.Tag{ + // Key: ExpiryTag, + // Value: FreePlanExpiry.String(), + // }, + // }, + // }, + // { + // ID: "pro-plan-rule", + // Status: StatusEnabled, + // Expiration: lifecycle.Expiration{ + // Days: lifecycle.ExpirationDays(expiryTimeConfig[ProPlanExpiry]), + // }, + // RuleFilter: lifecycle.Filter{ + // Tag: lifecycle.Tag{ + // Key: ExpiryTag, + // Value: ProPlanExpiry.String(), + // }, + // }, + // }, + // { + // ID: "team-plan-rule", + // Status: StatusEnabled, + // Expiration: lifecycle.Expiration{ + // Days: lifecycle.ExpirationDays(expiryTimeConfig[TeamPlanExpiry]), + // }, + // RuleFilter: lifecycle.Filter{ + // Tag: lifecycle.Tag{ + // Key: ExpiryTag, + // Value: TeamPlanExpiry.String(), + // }, + // }, + // }, + } + for _, expiryRule := range expiryRules { + lccfg.Rules = append(lccfg.Rules, lifecycle.Rule{ + ID: expiryRule.Tag, Status: StatusEnabled, Expiration: lifecycle.Expiration{ - Days: lifecycle.ExpirationDays(expiryTimeConfig[TeamPlanExpiry]), + Days: lifecycle.ExpirationDays(expiryRule.ExpirationDays), }, RuleFilter: lifecycle.Filter{ Tag: lifecycle.Tag{ Key: ExpiryTag, - Value: TeamPlanExpiry.String(), + Value: expiryRule.Tag, }, }, - }, + }) } err = client.SetBucketLifecycle(ctx, cfg.BucketName, lccfg) diff --git a/minio/minio_test.go b/minio/minio_test.go index 1b06c98..6a18e3d 100644 --- a/minio/minio_test.go +++ b/minio/minio_test.go @@ -50,4 +50,11 @@ func TestMinio(t *testing.T) { fileBytes, err := mc.GetFile(ctx, log, fileName.String()) require.NoError(t, err) require.Equal(t, jsonBytes, fileBytes) + + err = mc.DeleteFile(ctx, log, fileName.String()) + require.NoError(t, err) + + fileBytes, err = mc.GetFile(ctx, log, fileName.String()) + require.Error(t, err) + }