You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

newloadp.go 2.4 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package cmdline
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/jedib0t/go-pretty/v6/table"
  9. "github.com/spf13/cobra"
  10. cdssdk "gitlink.org.cn/cloudream/storage2/client/types"
  11. )
  12. func init() {
  13. cmd := &cobra.Command{
  14. Use: "newloadp localPath bucketID packageName userSpaceID...",
  15. Short: "Create a new package then upload an load files to it at the same time",
  16. Args: cobra.MinimumNArgs(4),
  17. Run: func(cmd *cobra.Command, args []string) {
  18. cmdCtx := GetCmdCtx(cmd)
  19. localPath := args[0]
  20. bktID, err := strconv.ParseInt(args[1], 10, 64)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. packageName := args[2]
  26. spaceIDs := make([]cdssdk.UserSpaceID, 0)
  27. rootPathes := make([]string, 0)
  28. for _, dst := range args[3:] {
  29. comps := strings.Split(dst, ":")
  30. if len(comps) != 2 {
  31. fmt.Println("invalid storage destination: ", dst)
  32. return
  33. }
  34. sID, err := strconv.ParseInt(comps[0], 10, 64)
  35. if err != nil {
  36. fmt.Println(err)
  37. return
  38. }
  39. spaceIDs = append(spaceIDs, cdssdk.UserSpaceID(sID))
  40. rootPathes = append(rootPathes, comps[1])
  41. }
  42. newloadp(cmdCtx, localPath, cdssdk.BucketID(bktID), packageName, spaceIDs, rootPathes)
  43. },
  44. }
  45. RootCmd.AddCommand(cmd)
  46. }
  47. func newloadp(cmdCtx *CommandContext, path string, bucketID cdssdk.BucketID, packageName string, spaceIDs []cdssdk.UserSpaceID, rootPathes []string) {
  48. up, err := cmdCtx.Cmdline.Svc.Uploader.BeginCreateLoad(bucketID, packageName, spaceIDs, rootPathes)
  49. if err != nil {
  50. fmt.Println(err)
  51. return
  52. }
  53. defer up.Abort()
  54. var fileCount int
  55. var totalSize int64
  56. err = filepath.WalkDir(path, func(fname string, fi os.DirEntry, err error) error {
  57. if err != nil {
  58. return nil
  59. }
  60. if fi.IsDir() {
  61. return nil
  62. }
  63. fileCount++
  64. info, err := fi.Info()
  65. if err != nil {
  66. return err
  67. }
  68. totalSize += info.Size()
  69. file, err := os.Open(fname)
  70. if err != nil {
  71. return err
  72. }
  73. defer file.Close()
  74. return up.Upload(fname, info.Size(), file)
  75. })
  76. if err != nil {
  77. fmt.Println(err.Error())
  78. return
  79. }
  80. ret, err := up.Commit()
  81. if err != nil {
  82. fmt.Printf("committing package: %v\n", err)
  83. return
  84. }
  85. wr := table.NewWriter()
  86. wr.AppendHeader(table.Row{"ID", "Name", "FileCount", "TotalSize"})
  87. wr.AppendRow(table.Row{ret.Package.PackageID, ret.Package.Name, fileCount, totalSize})
  88. fmt.Println(wr.Render())
  89. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。