upload.go 3.5 KB

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