欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

七牛api客户端示例

程序员文章站 2022-05-29 22:22:16
...
package main

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/qiniupd/qiniu-go-sdk/syncdata/operation"
	"github.com/qiniupd/qiniu-go-sdk/x/log.v7"
	"github.com/urfave/cli/v2"
)

var List = &cli.Command{
	Name:      "list",
	Aliases:   []string{"l"},
	Usage:     "list object storaged at qiniu cluster",
	UsageText: "list [--id=objectid]",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:   "id",
			Hidden: true,
			Value:  "",
		},
	},

	Action: func(cctx *cli.Context) error {
		cfgfile := "cfg.toml"

		//加载配置
		config, err := operation.Load(cfgfile)
		if err != nil {
			log.Println("加载配置文件失败:", err)
			return nil
		}

		op := operation.NewLister(config)
		objid := cctx.String("id")
		a := op.ListPrefix(objid)
		lister := operation.NewLister(config)
		filestats := lister.ListStat(a)
		for _, v := range filestats {
			//log.Println("filestat:", v)
			fmt.Printf("name is: %s, size is: %d\r\n", v.Name, v.Size)
		}
		return nil
	},
}

// 模糊删除
var DeleteBlurryMatch = &cli.Command{
	Name:      "deleteblurry",
	Aliases:   []string{"delblu"},
	Usage:     "delete the blurry match object storaged at qiniu cluster",
	UsageText: "deleteblurry [--id=objectid]",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:   "id",
			Hidden: true,
			Value:  "",
		},
	},

	Action: func(cctx *cli.Context) error {
		cfgfile := "cfg.toml"

		//加载配置
		config, err := operation.Load(cfgfile)
		if err != nil {
			log.Println("加载配置文件失败:", err)
			return nil
		}

		op := operation.NewLister(config)
		objid := cctx.String("id")
		a := op.ListPrefix("")
		lister := operation.NewLister(config)
		filestats := lister.ListStat(a)
		for _, v := range filestats {
			if strings.Contains(v.Name, objid) {

				fmt.Println("delete file: ", v.Name)
				err := op.Delete(v.Name)
				if err != nil {
					fmt.Printf("failed delete obj!, name: %s, size: %d\r\n", v.Name, v.Size)
				}
			}
		}

		return nil
	},
}

var Delete = &cli.Command{
	Name:      "delete",
	Aliases:   []string{"del"},
	Usage:     "delete the object storaged at qiniu cluster",
	UsageText: "delete [--id=objectid]",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:   "id",
			Hidden: true,
			Value:  "",
		},
	},

	Action: func(cctx *cli.Context) error {
		cfgfile := "cfg.toml"

		//加载配置
		config, err := operation.Load(cfgfile)
		if err != nil {
			log.Println("加载配置文件失败:", err)
			return nil
		}

		op := operation.NewLister(config)
		objid := cctx.String("id")
		a := op.ListPrefix(objid)
		lister := operation.NewLister(config)
		filestats := lister.ListStat(a)
		for _, v := range filestats {
			err := op.Delete(v.Name)
			if err != nil {
				fmt.Printf("failed delete obj!, name: %s, size: %d\r\n", v.Name, v.Size)
			}
		}
		return nil
	},
}

var Download = &cli.Command{
	Name:      "download",
	Aliases:   []string{"down"},
	Usage:     "download the object from  qiniu cluster",
	UsageText: "download [--id=objectid]",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:   "id",
			Hidden: true,
			Value:  "",
		},
		&cli.StringFlag{
			Name:   "output",
			Hidden: true,
			Value:  "./tmp.2k",
		},
	},

	Action: func(cctx *cli.Context) error {
		cfgfile := "cfg.toml"
		config, err := operation.Load(cfgfile)
		if err != nil {
			log.Println("加载配置文件失败:", err)
			return nil
		}

		objid := cctx.String("id")
		output := cctx.String("output")
		if objid == "" || output == "" {
			return errors.New("please input --id= --output=")
		}
		downloader := operation.NewDownloader(config)
		_, err = downloader.DownloadFile(objid, output)
		if err != nil {
			fmt.Printf("key %s , file %s err %v\n", objid, output, err)
		}

		return err
	},
}
var Upload = &cli.Command{
	Name:      "upload",
	Aliases:   []string{"up"},
	Usage:     "upload the object to qiniu cluster",
	UsageText: "upload [--id=objectid --name=name]",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:   "id",
			Hidden: true,
			Value:  "",
		},
		&cli.StringFlag{
			Name:   "name",
			Hidden: true,
			Value:  "./tmp.2k",
		},
	},

	Action: func(cctx *cli.Context) error {
		cfgfile := "cfg.toml"
		config, err := operation.Load(cfgfile)
		if err != nil {
			log.Println("加载配置文件失败:", err)
			return nil
		}

		objid := cctx.String("id")
		output := cctx.String("name")
		if objid == "" || output == "" {
			return errors.New("please input --id= --name=")
		}
		// 下载对象
		uploader := operation.NewUploader(config)
		err = uploader.Upload(objid, output)
		if err != nil {
			fmt.Printf("key %s , file %s err %v\n", objid, output, err)
		}

		return err
	},
}

func main() {
	local := []*cli.Command{
		List,
		DeleteBlurryMatch,
		Delete,
		Download,
		Upload,
	}

	app := &cli.App{
		Name:                 "qiniu-oss",
		Usage:                "manager qiniu oss cluster",
		Version:              "v0.0.0",
		EnableBashCompletion: true,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "configfile",
				EnvVars: []string{"QINIU"},
				Hidden:  true,
				Value:   "./cfg.toml",
			},
		},
		Commands: local,
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
		os.Exit(1)
	}

}