Skip to content

Commit

Permalink
add --metadata and --tag during put operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fatpat committed Nov 21, 2024
1 parent 44b87b6 commit 21f203a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <key>=<value>. 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 <key>=<value>. 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 {
Expand Down
42 changes: 41 additions & 1 deletion cli/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -71,17 +75,53 @@ 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"),
SendContentMd5: ctx.Bool("md5"),
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 rand_n int

Check failure on line 105 in cli/put.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; var rand_n should be randN (revive)
if _, err := fmt.Sscanf(value, "rand:%d", &rand_n); err == nil {
rng := rand.New(rand.NewSource(int64(rand.Uint64())))
value = ""
for i := 0; i < rand_n; i++ {
value = value + string(metadataChars[rng.Int()%len(metadataChars)])

Check failure on line 110 in cli/put.go

View workflow job for this annotation

GitHub Actions / lint

assignOp: replace `value = value + string(metadataChars[rng.Int()%len(metadataChars)])` with `value += string(metadataChars[rng.Int()%len(metadataChars)])` (gocritic)
}
}
values[key] = value
}

switch flag {
case "metadata":
options.UserMetadata = values
case "tag":
options.UserTags = values
}
}

return options
}

func checkPutSyntax(ctx *cli.Context) {
Expand Down

0 comments on commit 21f203a

Please sign in to comment.