func()

in oss/lib/object_tagging.go [182:246]


func (otc *ObjectTagCommand) RunCommand() error {
	otc.tagging.Tags = []oss.Tag{} // clear tags for test
	otc.monitor.init("ObjectTagging")
	encodingType, _ := GetString(OptionEncodingType, otc.command.options)
	cloudUrL, err := GetCloudUrl(otc.command.args[0], encodingType)
	if err != nil {
		return err
	}

	strMethod, _ := GetString(OptionMethod, otc.command.options)
	if strMethod == "" {
		return fmt.Errorf("--method value is empty")
	}

	strMethod = strings.ToLower(strMethod)
	if strMethod != "put" && strMethod != "get" && strMethod != "delete" {
		return fmt.Errorf("--method value is not in the optional value:put|get|delete")
	}
	otc.method = strMethod

	payer, _ := GetString(OptionRequestPayer, otc.command.options)
	if payer != "" {
		if payer != strings.ToLower(string(oss.Requester)) {
			return fmt.Errorf("invalid request payer: %s, please check", payer)
		}
		otc.commonOptions = append(otc.commonOptions, oss.RequestPayer(oss.PayerType(payer)))
	}

	recursive, _ := GetBool(OptionRecursion, otc.command.options)
	versionId, _ := GetString(OptionVersionId, otc.command.options)
	if len(versionId) > 0 {
		if recursive {
			return fmt.Errorf("--version-id and -r can't be both used")
		} else {
			otc.commonOptions = append(otc.commonOptions, oss.VersionId(versionId))
		}
	}

	if strMethod == "put" {
		if len(otc.command.args) < 2 {
			return fmt.Errorf("When the method value is put, there must be at least 2 parameters")
		}

		tagList := otc.command.args[1:len(otc.command.args)]
		for _, tag := range tagList {
			pSlice := strings.Split(tag, "#")
			if len(pSlice) != 2 {
				return fmt.Errorf("%s error,tag name and tag value must be separated by #", tag)
			}
			otc.tagging.Tags = append(otc.tagging.Tags, oss.Tag{Key: pSlice[0], Value: pSlice[1]})
		}
	}

	bucket, err := otc.command.ossBucket(cloudUrL.bucket)
	if err != nil {
		return err
	}

	if !recursive {
		err = otc.SingleObjectTagging(bucket, cloudUrL.object)
	} else {
		err = otc.BatchObjectTagging(bucket, *cloudUrL)
	}
	return err
}