From 9728e3e3e22fc4ba2c4db24955a8f6ea1209add7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20LOYET?= <822436+fatpat@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:50:53 +0100 Subject: [PATCH] add --metadata and --tag during put operations --- cli/flags.go | 10 ++++++++++ cli/put.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/cli/flags.go b/cli/flags.go index bcf562d..b30b6a0 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -273,6 +273,16 @@ var ioFlags = []cli.Flag{ Name: "lookup", Usage: "Force requests to be 'host' for host-style or 'path' for path-style lookup. Default will attempt autodetect based on remote host name.", }, + cli.StringSliceFlag{ + Name: "metadata", + Usage: "Add user metada to all objects using the format =. Random value can be set with 'rand:%length'. Can be used multiple times. Example: --metadata foo=bar --metadata randomValue=rand:1024.", + Hidden: true, + }, + cli.StringSliceFlag{ + Name: "tag", + Usage: "Add user tag to all objects using the format =. Random value can be set with 'rand:%length'. Can be used multiple times. Example: --tag foo=bar --tag randomValue=rand:1024.", + Hidden: true, + }, } func getCommon(ctx *cli.Context, src func() generator.Source) bench.Common { diff --git a/cli/put.go b/cli/put.go index 9c907bb..c424014 100644 --- a/cli/put.go +++ b/cli/put.go @@ -18,6 +18,10 @@ package cli import ( + "fmt" + "math/rand" + "strings" + "github.com/minio/cli" "github.com/minio/minio-go/v7" "github.com/minio/pkg/v2/console" @@ -73,10 +77,12 @@ func mainPut(ctx *cli.Context) error { return runBench(ctx, &b) } +const metadataChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_." + // putOpts retrieves put options from the context. func putOpts(ctx *cli.Context) minio.PutObjectOptions { pSize, _ := toSize(ctx.String("part.size")) - return minio.PutObjectOptions{ + options := minio.PutObjectOptions{ ServerSideEncryption: newSSE(ctx), DisableMultipart: ctx.Bool("disable-multipart"), DisableContentSha256: ctx.Bool("disable-sha256-payload"), @@ -84,6 +90,40 @@ func putOpts(ctx *cli.Context) minio.PutObjectOptions { StorageClass: ctx.String("storage-class"), PartSize: pSize, } + + for _, flag := range []string{"metadata", "tag"} { + values := make(map[string]string) + + for _, v := range ctx.StringSlice(flag) { + idx := strings.Index(v, "=") + if idx <= 0 { + console.Fatalf("--%s takes `key=value` argument", flag) + } + key := v[:idx] + value := v[idx+1:] + if len(value) == 0 { + console.Fatal("--%s value can't be empty", flag) + } + var randN int + if _, err := fmt.Sscanf(value, "rand:%d", &randN); err == nil { + rng := rand.New(rand.NewSource(int64(rand.Uint64()))) + value = "" + for i := 0; i < randN; i++ { + value += string(metadataChars[rng.Int()%len(metadataChars)]) + } + } + values[key] = value + } + + switch flag { + case "metadata": + options.UserMetadata = values + case "tag": + options.UserTags = values + } + } + + return options } func checkPutSyntax(ctx *cli.Context) {