Skip to content

Commit

Permalink
add --metadata to add user metadata during put
Browse files Browse the repository at this point in the history
  • Loading branch information
fatpat committed Nov 19, 2024
1 parent 3d18966 commit c907159
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ var ioFlags = []cli.Flag{
Value: 0,
Usage: "Rate limit each instance to this number of requests per second (0 to disable)",
},
cli.StringSliceFlag{
Name: "metadata",
Usage: "Set 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.",
},
}

func getCommon(ctx *cli.Context, src func() generator.Source) bench.Common {
Expand Down
30 changes: 30 additions & 0 deletions cli/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
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,16 +74,43 @@ 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"))

/* metadatas */
metadatas := make(map[string]string)
for _, v := range ctx.StringSlice("metadata") {
idx := strings.Index(v, "=")
if idx <= 0 {
console.Fatal("--metadata takes `key=value` argument")
}
key := v[:idx]
value := v[idx+1:]
if len(value) == 0 {
console.Fatal("--metadata value can't be empty")
}
var rand_n int;
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)])
}
}
metadatas[key] = value
}

return 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,
UserMetadata: metadatas,
}
}

Expand Down

0 comments on commit c907159

Please sign in to comment.