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

108 lines
3.9 KiB

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