upload.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package command
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/spf13/viper"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. )
  12. var (
  13. upload UploadOptions
  14. )
  15. type UploadOptions struct {
  16. master *string
  17. dir *string
  18. include *string
  19. replication *string
  20. collection *string
  21. dataCenter *string
  22. ttl *string
  23. maxMB *int
  24. }
  25. func init() {
  26. cmdUpload.Run = runUpload // break init cycle
  27. cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
  28. upload.master = cmdUpload.Flag.String("master", "localhost:9333", "SeaweedFS master location")
  29. upload.dir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
  30. upload.include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
  31. upload.replication = cmdUpload.Flag.String("replication", "", "replication type")
  32. upload.collection = cmdUpload.Flag.String("collection", "", "optional collection name")
  33. upload.dataCenter = cmdUpload.Flag.String("dataCenter", "", "optional data center name")
  34. upload.ttl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
  35. upload.maxMB = cmdUpload.Flag.Int("maxMB", 32, "split files larger than the limit")
  36. }
  37. var cmdUpload = &Command{
  38. UsageLine: "upload -master=localhost:9333 file1 [file2 file3]\n weed upload -master=localhost:9333 -dir=one_directory -include=*.pdf",
  39. Short: "upload one or a list of files",
  40. Long: `upload one or a list of files, or batch upload one whole folder recursively.
  41. If uploading a list of files:
  42. It uses consecutive file keys for the list of files.
  43. e.g. If the file1 uses key k, file2 can be read via k_1
  44. If uploading a whole folder recursively:
  45. All files under the folder and subfolders will be uploaded, each with its own file key.
  46. Optional parameter "-include" allows you to specify the file name patterns.
  47. If "maxMB" is set to a positive number, files larger than it would be split into chunks and uploaded separately.
  48. The list of file ids of those chunks would be stored in an additional chunk, and this additional chunk's file id would be returned.
  49. `,
  50. }
  51. func runUpload(cmd *Command, args []string) bool {
  52. util.LoadConfiguration("security", false)
  53. grpcDialOption := security.LoadClientTLS(viper.Sub("grpc"), "client")
  54. if len(args) == 0 {
  55. if *upload.dir == "" {
  56. return false
  57. }
  58. filepath.Walk(*upload.dir, func(path string, info os.FileInfo, err error) error {
  59. if err == nil {
  60. if !info.IsDir() {
  61. if *upload.include != "" {
  62. if ok, _ := filepath.Match(*upload.include, filepath.Base(path)); !ok {
  63. return nil
  64. }
  65. }
  66. parts, e := operation.NewFileParts([]string{path})
  67. if e != nil {
  68. return e
  69. }
  70. results, e := operation.SubmitFiles(*upload.master, grpcDialOption, parts,
  71. *upload.replication, *upload.collection, *upload.dataCenter,
  72. *upload.ttl, *upload.maxMB)
  73. bytes, _ := json.Marshal(results)
  74. fmt.Println(string(bytes))
  75. if e != nil {
  76. return e
  77. }
  78. }
  79. } else {
  80. fmt.Println(err)
  81. }
  82. return err
  83. })
  84. } else {
  85. parts, e := operation.NewFileParts(args)
  86. if e != nil {
  87. fmt.Println(e.Error())
  88. }
  89. results, _ := operation.SubmitFiles(*upload.master, grpcDialOption, parts,
  90. *upload.replication, *upload.collection, *upload.dataCenter,
  91. *upload.ttl, *upload.maxMB)
  92. bytes, _ := json.Marshal(results)
  93. fmt.Println(string(bytes))
  94. }
  95. return true
  96. }