2022-06-01 12:42:28 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2022-10-18 11:43:04 +00:00
|
|
|
"bytes"
|
2022-06-01 12:42:28 +00:00
|
|
|
"fmt"
|
2022-10-18 11:43:04 +00:00
|
|
|
"io"
|
2022-06-01 12:42:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2024-11-07 14:32:10 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-06-01 12:42:28 +00:00
|
|
|
"github.com/cheggaaa/pb"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-06-29 06:48:32 +00:00
|
|
|
noProgressFlag = "no-progress"
|
|
|
|
notificationFlag = "notify"
|
|
|
|
copiesNumberFlag = "copies-number"
|
|
|
|
prepareLocallyFlag = "prepare-locally"
|
2022-06-01 12:42:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var putExpiredOn uint64
|
|
|
|
|
|
|
|
var objectPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
2023-01-27 10:44:39 +00:00
|
|
|
Short: "Put object to FrostFS",
|
|
|
|
Long: "Put object to FrostFS",
|
2022-06-01 12:42:28 +00:00
|
|
|
Run: putObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectPutCmd() {
|
|
|
|
commonflags.Init(objectPutCmd)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectPutCmd, "PUT")
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags := objectPutCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(fileFlag, "", "File with object payload")
|
|
|
|
_ = objectPutCmd.MarkFlagFilename(fileFlag)
|
|
|
|
_ = objectPutCmd.MarkFlagRequired(fileFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags.String("attributes", "", "User attributes in form of Key1=Value1,Key2=Value2")
|
|
|
|
flags.Bool("disable-filename", false, "Do not set well-known filename attribute")
|
|
|
|
flags.Bool("disable-timestamp", false, "Do not set well-known timestamp attribute")
|
2022-12-21 07:59:35 +00:00
|
|
|
flags.Uint64VarP(&putExpiredOn, commonflags.ExpireAt, "e", 0, "The last active epoch in the life of the object")
|
2022-06-01 12:42:28 +00:00
|
|
|
flags.Bool(noProgressFlag, false, "Do not show progress bar")
|
2023-06-29 06:48:32 +00:00
|
|
|
flags.Bool(prepareLocallyFlag, false, "Generate object header on the client side (for big object - split locally too)")
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags.String(notificationFlag, "", "Object notification in the form of *epoch*:*topic*; '-' topic means using default")
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.Bool(binaryFlag, false, "Deserialize object structure from given file.")
|
2023-05-15 12:24:29 +00:00
|
|
|
|
|
|
|
flags.String(copiesNumberFlag, "", "Number of copies of the object to store within the RPC call")
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func putObject(cmd *cobra.Command, _ []string) {
|
2022-10-18 11:43:04 +00:00
|
|
|
binary, _ := cmd.Flags().GetBool(binaryFlag)
|
|
|
|
cidVal, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
|
|
|
|
|
|
|
if !binary && cidVal == "" {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("required flag \"%s\" not set", commonflags.CIDFlag))
|
2022-10-18 11:43:04 +00:00
|
|
|
}
|
2022-06-01 12:42:28 +00:00
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
|
|
|
var ownerID user.ID
|
|
|
|
var cnr cid.ID
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
filename, _ := cmd.Flags().GetString(fileFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
2022-10-18 11:43:04 +00:00
|
|
|
var payloadReader io.Reader = f
|
2023-07-06 12:36:41 +00:00
|
|
|
obj := objectSDK.New()
|
2022-10-18 11:43:04 +00:00
|
|
|
|
|
|
|
if binary {
|
2023-03-23 12:14:44 +00:00
|
|
|
payloadReader, cnr, ownerID = readFilePayload(filename, cmd)
|
2022-10-18 11:43:04 +00:00
|
|
|
} else {
|
|
|
|
readCID(cmd, &cnr)
|
|
|
|
user.IDFromKey(&ownerID, pk.PublicKey)
|
|
|
|
}
|
2022-06-01 12:42:28 +00:00
|
|
|
|
2023-03-23 12:14:44 +00:00
|
|
|
attrs := getAllObjectAttributes(cmd)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
obj.SetContainerID(cnr)
|
2023-11-21 08:42:30 +00:00
|
|
|
obj.SetOwnerID(ownerID)
|
2022-06-01 12:42:28 +00:00
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
|
|
|
|
notificationInfo, err := parseObjectNotifications(cmd)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't parse object notification information: %w", err)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
if notificationInfo != nil {
|
|
|
|
obj.SetNotification(*notificationInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm internalclient.PutObjectPrm
|
2023-06-29 06:48:32 +00:00
|
|
|
if prepareLocally, _ := cmd.Flags().GetBool(prepareLocallyFlag); prepareLocally {
|
2023-07-04 15:41:25 +00:00
|
|
|
prm.SetClient(internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC))
|
2023-06-29 06:48:32 +00:00
|
|
|
prm.PrepareLocally()
|
|
|
|
} else {
|
|
|
|
ReadOrOpenSession(cmd, &prm, pk, cnr, nil)
|
|
|
|
}
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
|
|
|
prm.SetHeader(obj)
|
|
|
|
|
|
|
|
var p *pb.ProgressBar
|
|
|
|
|
|
|
|
noProgress, _ := cmd.Flags().GetBool(noProgressFlag)
|
|
|
|
if noProgress {
|
2022-10-18 11:43:04 +00:00
|
|
|
prm.SetPayloadReader(payloadReader)
|
2022-06-01 12:42:28 +00:00
|
|
|
} else {
|
2022-10-18 11:43:04 +00:00
|
|
|
if binary {
|
2023-03-23 12:14:44 +00:00
|
|
|
p = setBinaryPayloadReader(cmd, obj, &prm, payloadReader)
|
2022-10-18 11:43:04 +00:00
|
|
|
} else {
|
2023-03-23 12:14:44 +00:00
|
|
|
p = setFilePayloadReader(cmd, f, &prm)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:24:29 +00:00
|
|
|
copyNum, err := cmd.Flags().GetString(copiesNumberFlag)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't parse object copies numbers information: %w", err)
|
2023-06-29 06:48:32 +00:00
|
|
|
prm.SetCopiesNumberByVectors(parseCopyNumber(cmd, copyNum))
|
2023-05-15 12:24:29 +00:00
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.PutObject(cmd.Context(), prm)
|
2022-06-01 12:42:28 +00:00
|
|
|
if p != nil {
|
|
|
|
p.Finish()
|
|
|
|
}
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-06-08 07:59:11 +00:00
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
cmd.Printf("[%s] Object successfully stored\n", filename)
|
2022-08-19 17:52:29 +00:00
|
|
|
cmd.Printf(" OID: %s\n CID: %s\n", res.ID(), cnr)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:48:32 +00:00
|
|
|
func parseCopyNumber(cmd *cobra.Command, copyNum string) []uint32 {
|
|
|
|
var cn []uint32
|
|
|
|
if len(copyNum) > 0 {
|
|
|
|
for _, num := range strings.Split(copyNum, ",") {
|
|
|
|
val, err := strconv.ParseUint(num, 10, 32)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't parse object copies numbers information: %w", err)
|
|
|
|
cn = append(cn, uint32(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:14:44 +00:00
|
|
|
func readFilePayload(filename string, cmd *cobra.Command) (io.Reader, cid.ID, user.ID) {
|
|
|
|
buf, err := os.ReadFile(filename)
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to read given file: %w", err)
|
2023-07-06 12:36:41 +00:00
|
|
|
objTemp := objectSDK.New()
|
2023-03-23 12:14:44 +00:00
|
|
|
// TODO(@acid-ant): #1932 Use streams to marshal/unmarshal payload
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't unmarshal object from given file: %w", objTemp.Unmarshal(buf))
|
|
|
|
payloadReader := bytes.NewReader(objTemp.Payload())
|
|
|
|
cnr, _ := objTemp.ContainerID()
|
2023-11-21 08:42:30 +00:00
|
|
|
ownerID := objTemp.OwnerID()
|
2023-03-23 12:14:44 +00:00
|
|
|
return payloadReader, cnr, ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFilePayloadReader(cmd *cobra.Command, f *os.File, prm *internalclient.PutObjectPrm) *pb.ProgressBar {
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
cmd.PrintErrf("Failed to get file size, progress bar is disabled: %v\n", err)
|
|
|
|
prm.SetPayloadReader(f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := pb.New64(fi.Size())
|
|
|
|
p.Output = cmd.OutOrStdout()
|
|
|
|
prm.SetPayloadReader(p.NewProxyReader(f))
|
2024-03-11 14:11:49 +00:00
|
|
|
prm.SetHeaderCallback(func() { p.Start() })
|
2023-03-23 12:14:44 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func setBinaryPayloadReader(cmd *cobra.Command, obj *objectSDK.Object, prm *internalclient.PutObjectPrm, payloadReader io.Reader) *pb.ProgressBar {
|
2023-03-23 12:14:44 +00:00
|
|
|
p := pb.New(len(obj.Payload()))
|
|
|
|
p.Output = cmd.OutOrStdout()
|
|
|
|
prm.SetPayloadReader(p.NewProxyReader(payloadReader))
|
2024-03-11 14:11:49 +00:00
|
|
|
prm.SetHeaderCallback(func() { p.Start() })
|
2023-03-23 12:14:44 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func getAllObjectAttributes(cmd *cobra.Command) []objectSDK.Attribute {
|
2023-03-23 12:14:44 +00:00
|
|
|
attrs, err := parseObjectAttrs(cmd)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't parse object attributes: %w", err)
|
|
|
|
|
|
|
|
expiresOn, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
|
|
|
|
if expiresOn > 0 {
|
|
|
|
var expAttrFound bool
|
|
|
|
expAttrValue := strconv.FormatUint(expiresOn, 10)
|
|
|
|
|
|
|
|
for i := range attrs {
|
|
|
|
if attrs[i].Key() == objectV2.SysAttributeExpEpoch {
|
|
|
|
attrs[i].SetValue(expAttrValue)
|
|
|
|
expAttrFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expAttrFound {
|
|
|
|
index := len(attrs)
|
2023-07-06 12:36:41 +00:00
|
|
|
attrs = append(attrs, objectSDK.Attribute{})
|
2023-03-23 12:14:44 +00:00
|
|
|
attrs[index].SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
attrs[index].SetValue(expAttrValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func parseObjectAttrs(cmd *cobra.Command) ([]objectSDK.Attribute, error) {
|
2022-06-01 12:42:28 +00:00
|
|
|
var rawAttrs []string
|
|
|
|
|
|
|
|
raw := cmd.Flag("attributes").Value.String()
|
|
|
|
if len(raw) != 0 {
|
|
|
|
rawAttrs = strings.Split(raw, ",")
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
attrs := make([]objectSDK.Attribute, len(rawAttrs), len(rawAttrs)+2) // name + timestamp attributes
|
2022-06-01 12:42:28 +00:00
|
|
|
for i := range rawAttrs {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(rawAttrs[i], "=")
|
|
|
|
if !found {
|
2022-06-01 12:42:28 +00:00
|
|
|
return nil, fmt.Errorf("invalid attribute format: %s", rawAttrs[i])
|
|
|
|
}
|
2023-02-27 14:19:35 +00:00
|
|
|
attrs[i].SetKey(k)
|
|
|
|
attrs[i].SetValue(v)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disableFilename, _ := cmd.Flags().GetBool("disable-filename")
|
|
|
|
if !disableFilename {
|
2022-10-18 11:43:04 +00:00
|
|
|
filename := filepath.Base(cmd.Flag(fileFlag).Value.String())
|
2022-06-01 12:42:28 +00:00
|
|
|
index := len(attrs)
|
2023-07-06 12:36:41 +00:00
|
|
|
attrs = append(attrs, objectSDK.Attribute{})
|
|
|
|
attrs[index].SetKey(objectSDK.AttributeFileName)
|
2022-06-01 12:42:28 +00:00
|
|
|
attrs[index].SetValue(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
disableTime, _ := cmd.Flags().GetBool("disable-timestamp")
|
|
|
|
if !disableTime {
|
|
|
|
index := len(attrs)
|
2023-07-06 12:36:41 +00:00
|
|
|
attrs = append(attrs, objectSDK.Attribute{})
|
|
|
|
attrs[index].SetKey(objectSDK.AttributeTimestamp)
|
2022-06-01 12:42:28 +00:00
|
|
|
attrs[index].SetValue(strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func parseObjectNotifications(cmd *cobra.Command) (*objectSDK.NotificationInfo, error) {
|
2022-06-01 12:42:28 +00:00
|
|
|
const (
|
|
|
|
separator = ":"
|
|
|
|
useDefaultTopic = "-"
|
|
|
|
)
|
|
|
|
|
|
|
|
raw := cmd.Flag(notificationFlag).Value.String()
|
|
|
|
if raw == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
before, after, found := strings.Cut(raw, separator)
|
|
|
|
if !found {
|
2022-06-01 12:42:28 +00:00
|
|
|
return nil, fmt.Errorf("notification must be in the form of: *epoch*%s*topic*, got %s", separator, raw)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
ni := new(objectSDK.NotificationInfo)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
epoch, err := strconv.ParseUint(before, 10, 64)
|
2022-06-01 12:42:28 +00:00
|
|
|
if err != nil {
|
2023-02-27 14:19:35 +00:00
|
|
|
return nil, fmt.Errorf("could not parse notification epoch %s: %w", before, err)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ni.SetEpoch(epoch)
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
if after == "" {
|
2022-06-01 12:42:28 +00:00
|
|
|
return nil, fmt.Errorf("incorrect empty topic: use %s to force using default topic", useDefaultTopic)
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
if after != useDefaultTopic {
|
|
|
|
ni.SetTopic(after)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ni, nil
|
|
|
|
}
|