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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/common/sdks/storage"
  11. )
  12. func init() {
  13. cmd := &cobra.Command{
  14. Use: "newloadp localPath bucketID packageName storageID...",
  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. storageIDs := make([]cdssdk.StorageID, 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. storageIDs = append(storageIDs, cdssdk.StorageID(sID))
  40. rootPathes = append(rootPathes, comps[1])
  41. }
  42. newloadp(cmdCtx, localPath, cdssdk.BucketID(bktID), packageName, storageIDs, rootPathes)
  43. },
  44. }
  45. rootCmd.AddCommand(cmd)
  46. }
  47. func newloadp(cmdCtx *CommandContext, path string, bucketID cdssdk.BucketID, packageName string, storageIDs []cdssdk.StorageID, rootPathes []string) {
  48. userID := cdssdk.UserID(1)
  49. up, err := cmdCtx.Cmdline.Svc.Uploader.BeginCreateLoad(userID, bucketID, packageName, storageIDs, rootPathes)
  50. if err != nil {
  51. fmt.Println(err)
  52. return
  53. }
  54. defer up.Abort()
  55. var fileCount int
  56. var totalSize int64
  57. err = filepath.WalkDir(path, func(fname string, fi os.DirEntry, err error) error {
  58. if err != nil {
  59. return nil
  60. }
  61. if fi.IsDir() {
  62. return nil
  63. }
  64. fileCount++
  65. info, err := fi.Info()
  66. if err != nil {
  67. return err
  68. }
  69. totalSize += info.Size()
  70. file, err := os.Open(fname)
  71. if err != nil {
  72. return err
  73. }
  74. defer file.Close()
  75. return up.Upload(fname, info.Size(), file)
  76. })
  77. if err != nil {
  78. fmt.Println(err.Error())
  79. return
  80. }
  81. ret, err := up.Commit()
  82. if err != nil {
  83. fmt.Printf("committing package: %v\n", err)
  84. return
  85. }
  86. wr := table.NewWriter()
  87. wr.AppendHeader(table.Row{"ID", "Name", "FileCount", "TotalSize"})
  88. wr.AppendRow(table.Row{ret.Package.PackageID, ret.Package.Name, fileCount, totalSize})
  89. fmt.Println(wr.Render())
  90. }

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