2016-06-03 01:09:14 +00:00
|
|
|
package command
|
2012-08-07 08:29:22 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-03 07:43:01 +00:00
|
|
|
"context"
|
2012-09-20 09:11:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2021-01-03 07:43:01 +00:00
|
|
|
"google.golang.org/grpc"
|
2012-09-20 09:11:08 +00:00
|
|
|
"os"
|
2013-07-13 21:14:16 +00:00
|
|
|
"path/filepath"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
2013-07-13 21:14:16 +00:00
|
|
|
var (
|
2015-06-02 02:25:01 +00:00
|
|
|
upload UploadOptions
|
2013-07-13 21:14:16 +00:00
|
|
|
)
|
2012-09-20 09:11:08 +00:00
|
|
|
|
2015-06-02 02:25:01 +00:00
|
|
|
type UploadOptions struct {
|
2020-03-18 17:50:53 +00:00
|
|
|
master *string
|
|
|
|
dir *string
|
|
|
|
include *string
|
|
|
|
replication *string
|
|
|
|
collection *string
|
|
|
|
dataCenter *string
|
|
|
|
ttl *string
|
2020-12-16 17:14:05 +00:00
|
|
|
diskType *string
|
2020-03-18 17:50:53 +00:00
|
|
|
maxMB *int
|
|
|
|
usePublicUrl *bool
|
2015-06-02 02:25:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func init() {
|
2012-09-20 09:11:08 +00:00
|
|
|
cmdUpload.Run = runUpload // break init cycle
|
2013-01-20 03:49:57 +00:00
|
|
|
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
2016-07-21 06:45:55 +00:00
|
|
|
upload.master = cmdUpload.Flag.String("master", "localhost:9333", "SeaweedFS master location")
|
2015-06-02 02:25:01 +00:00
|
|
|
upload.dir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
|
|
|
|
upload.include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
|
|
|
|
upload.replication = cmdUpload.Flag.String("replication", "", "replication type")
|
|
|
|
upload.collection = cmdUpload.Flag.String("collection", "", "optional collection name")
|
2017-01-09 15:34:07 +00:00
|
|
|
upload.dataCenter = cmdUpload.Flag.String("dataCenter", "", "optional data center name")
|
2021-02-22 10:03:12 +00:00
|
|
|
upload.diskType = cmdUpload.Flag.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
|
2015-06-02 02:25:01 +00:00
|
|
|
upload.ttl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
|
2021-04-01 09:21:59 +00:00
|
|
|
upload.maxMB = cmdUpload.Flag.Int("maxMB", 4, "split files larger than the limit")
|
2020-03-18 17:50:53 +00:00
|
|
|
upload.usePublicUrl = cmdUpload.Flag.Bool("usePublicUrl", false, "upload to public url from volume server")
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdUpload = &Command{
|
2017-08-10 17:26:36 +00:00
|
|
|
UsageLine: "upload -master=localhost:9333 file1 [file2 file3]\n weed upload -master=localhost:9333 -dir=one_directory -include=*.pdf",
|
2012-09-20 09:11:08 +00:00
|
|
|
Short: "upload one or a list of files",
|
2013-07-13 21:14:16 +00:00
|
|
|
Long: `upload one or a list of files, or batch upload one whole folder recursively.
|
2015-02-07 23:35:28 +00:00
|
|
|
|
2013-11-19 07:04:33 +00:00
|
|
|
If uploading a list of files:
|
2012-09-20 09:11:08 +00:00
|
|
|
It uses consecutive file keys for the list of files.
|
2012-08-07 08:29:22 +00:00
|
|
|
e.g. If the file1 uses key k, file2 can be read via k_1
|
|
|
|
|
2013-07-20 22:14:12 +00:00
|
|
|
If uploading a whole folder recursively:
|
|
|
|
All files under the folder and subfolders will be uploaded, each with its own file key.
|
|
|
|
Optional parameter "-include" allows you to specify the file name patterns.
|
2015-02-07 23:35:28 +00:00
|
|
|
|
2019-02-06 13:59:15 +00:00
|
|
|
If "maxMB" is set to a positive number, files larger than it would be split into chunks and uploaded separately.
|
2015-02-07 23:35:28 +00:00
|
|
|
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.
|
2013-07-20 22:14:12 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUpload(cmd *Command, args []string) bool {
|
2019-02-18 20:11:52 +00:00
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
util.LoadConfiguration("security", false)
|
2020-01-29 17:09:55 +00:00
|
|
|
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
|
2019-02-18 20:11:52 +00:00
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
defaultReplication, err := readMasterConfiguration(grpcDialOption, pb.ServerAddress(*upload.master))
|
2021-01-03 07:43:01 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("upload: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if *upload.replication == "" {
|
2021-08-03 16:32:31 +00:00
|
|
|
*upload.replication = defaultReplication
|
2021-01-03 07:43:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 03:23:40 +00:00
|
|
|
if len(args) == 0 {
|
2015-06-02 02:25:01 +00:00
|
|
|
if *upload.dir == "" {
|
2013-07-13 21:14:16 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-06-07 16:17:16 +00:00
|
|
|
err = filepath.Walk(util.ResolvePath(*upload.dir), func(path string, info os.FileInfo, err error) error {
|
2013-07-14 05:01:48 +00:00
|
|
|
if err == nil {
|
|
|
|
if !info.IsDir() {
|
2015-06-02 02:25:01 +00:00
|
|
|
if *upload.include != "" {
|
|
|
|
if ok, _ := filepath.Match(*upload.include, filepath.Base(path)); !ok {
|
2013-07-14 06:17:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 17:07:42 +00:00
|
|
|
parts, e := operation.NewFileParts([]string{path})
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2021-09-13 05:47:52 +00:00
|
|
|
results, e := operation.SubmitFiles(func() pb.ServerAddress { return pb.ServerAddress(*upload.master) }, grpcDialOption, parts, *upload.replication, *upload.collection, *upload.dataCenter, *upload.ttl, *upload.diskType, *upload.maxMB, *upload.usePublicUrl)
|
2013-07-14 05:01:48 +00:00
|
|
|
bytes, _ := json.Marshal(results)
|
|
|
|
fmt.Println(string(bytes))
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2013-07-13 21:14:16 +00:00
|
|
|
}
|
2013-07-14 05:01:48 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
2013-07-13 21:14:16 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2021-06-07 16:17:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2021-07-01 08:21:14 +00:00
|
|
|
return false
|
2021-06-07 16:17:16 +00:00
|
|
|
}
|
2013-07-13 21:14:16 +00:00
|
|
|
} else {
|
2013-08-14 17:07:42 +00:00
|
|
|
parts, e := operation.NewFileParts(args)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println(e.Error())
|
2021-06-07 16:17:16 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-09-13 05:47:52 +00:00
|
|
|
results, err := operation.SubmitFiles(func() pb.ServerAddress { return pb.ServerAddress(*upload.master) }, grpcDialOption, parts, *upload.replication, *upload.collection, *upload.dataCenter, *upload.ttl, *upload.diskType, *upload.maxMB, *upload.usePublicUrl)
|
2021-06-07 16:17:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return false
|
2013-08-14 17:07:42 +00:00
|
|
|
}
|
2013-07-13 21:35:29 +00:00
|
|
|
bytes, _ := json.Marshal(results)
|
|
|
|
fmt.Println(string(bytes))
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
|
|
|
return true
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
2021-01-03 07:43:01 +00:00
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func readMasterConfiguration(grpcDialOption grpc.DialOption, masterAddress pb.ServerAddress) (replication string, err error) {
|
2021-12-26 08:15:03 +00:00
|
|
|
err = pb.WithMasterClient(false, masterAddress, grpcDialOption, func(client master_pb.SeaweedClient) error {
|
2021-01-03 07:43:01 +00:00
|
|
|
resp, err := client.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get master %s configuration: %v", masterAddress, err)
|
|
|
|
}
|
|
|
|
replication = resp.DefaultReplication
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|