2016-07-21 06:45:55 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-18 20:11:52 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/server"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"google.golang.org/grpc"
|
2016-07-21 06:45:55 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2018-07-22 00:39:10 +00:00
|
|
|
"context"
|
2016-07-21 06:45:55 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2018-07-22 00:39:10 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"io"
|
2018-05-30 06:46:45 +00:00
|
|
|
"net/http"
|
2018-05-30 07:54:56 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-07-21 06:45:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
copy CopyOptions
|
|
|
|
)
|
|
|
|
|
|
|
|
type CopyOptions struct {
|
2019-02-18 20:11:52 +00:00
|
|
|
filerGrpcPort *int
|
|
|
|
master *string
|
|
|
|
include *string
|
|
|
|
replication *string
|
|
|
|
collection *string
|
|
|
|
ttl *string
|
|
|
|
maxMB *int
|
|
|
|
grpcDialOption grpc.DialOption
|
2016-07-21 06:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdCopy.Run = runCopy // break init cycle
|
|
|
|
cmdCopy.IsDebug = cmdCopy.Flag.Bool("debug", false, "verbose debug information")
|
|
|
|
copy.master = cmdCopy.Flag.String("master", "localhost:9333", "SeaweedFS master location")
|
|
|
|
copy.include = cmdCopy.Flag.String("include", "", "pattens of files to copy, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
|
|
|
|
copy.replication = cmdCopy.Flag.String("replication", "", "replication type")
|
|
|
|
copy.collection = cmdCopy.Flag.String("collection", "", "optional collection name")
|
|
|
|
copy.ttl = cmdCopy.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
|
|
|
|
copy.maxMB = cmdCopy.Flag.Int("maxMB", 0, "split files larger than the limit")
|
2018-06-06 06:37:41 +00:00
|
|
|
copy.filerGrpcPort = cmdCopy.Flag.Int("filer.port.grpc", 0, "filer grpc server listen port, default to filer port + 10000")
|
2016-07-21 06:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdCopy = &Command{
|
2016-07-21 22:00:07 +00:00
|
|
|
UsageLine: "filer.copy file_or_dir1 [file_or_dir2 file_or_dir3] http://localhost:8888/path/to/a/folder/",
|
2016-07-21 06:45:55 +00:00
|
|
|
Short: "copy one or a list of files to a filer folder",
|
|
|
|
Long: `copy one or a list of files, or batch copy one whole folder recursively, to a filer folder
|
|
|
|
|
|
|
|
It can copy one or a list of files or folders.
|
|
|
|
|
|
|
|
If copying a whole folder recursively:
|
|
|
|
All files under the folder and subfolders will be copyed.
|
|
|
|
Optional parameter "-include" allows you to specify the file name patterns.
|
|
|
|
|
2018-09-28 08:58:34 +00:00
|
|
|
If "maxMB" is set to a positive number, files larger than it would be split into chunks.
|
2016-07-21 06:45:55 +00:00
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCopy(cmd *Command, args []string) bool {
|
2019-02-18 20:11:52 +00:00
|
|
|
|
|
|
|
weed_server.LoadConfiguration("security", false)
|
|
|
|
|
2016-07-21 06:45:55 +00:00
|
|
|
if len(args) <= 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
filerDestination := args[len(args)-1]
|
2018-07-22 00:39:10 +00:00
|
|
|
fileOrDirs := args[0 : len(args)-1]
|
2016-07-21 06:45:55 +00:00
|
|
|
|
|
|
|
filerUrl, err := url.Parse(filerDestination)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("The last argument should be a URL on filer: %v\n", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-05-30 06:46:45 +00:00
|
|
|
urlPath := filerUrl.Path
|
|
|
|
if !strings.HasSuffix(urlPath, "/") {
|
2018-12-24 02:20:11 +00:00
|
|
|
fmt.Printf("The last argument should be a folder and end with \"/\": %v\n", err)
|
|
|
|
return false
|
2016-07-21 06:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 06:37:41 +00:00
|
|
|
if filerUrl.Port() == "" {
|
|
|
|
fmt.Printf("The filer port should be specified.\n")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
filerPort, parseErr := strconv.ParseUint(filerUrl.Port(), 10, 64)
|
|
|
|
if parseErr != nil {
|
|
|
|
fmt.Printf("The filer port parse error: %v\n", parseErr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
filerGrpcPort := filerPort + 10000
|
|
|
|
if *copy.filerGrpcPort != 0 {
|
|
|
|
filerGrpcPort = uint64(*copy.filerGrpcPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
filerGrpcAddress := fmt.Sprintf("%s:%d", filerUrl.Hostname(), filerGrpcPort)
|
2019-02-18 20:11:52 +00:00
|
|
|
copy.grpcDialOption = security.LoadClientTLS(viper.Sub("grpc"), "client")
|
2018-06-06 06:37:41 +00:00
|
|
|
|
2016-07-21 06:45:55 +00:00
|
|
|
for _, fileOrDir := range fileOrDirs {
|
2019-03-16 00:20:24 +00:00
|
|
|
if !doEachCopy(context.Background(), fileOrDir, filerUrl.Host, filerGrpcAddress, copy.grpcDialOption, urlPath) {
|
2016-07-21 06:45:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func doEachCopy(ctx context.Context, fileOrDir string, filerAddress, filerGrpcAddress string, grpcDialOption grpc.DialOption, path string) bool {
|
2016-07-21 06:45:55 +00:00
|
|
|
f, err := os.Open(fileOrDir)
|
|
|
|
if err != nil {
|
2018-05-14 06:56:16 +00:00
|
|
|
fmt.Printf("Failed to open file %s: %v\n", fileOrDir, err)
|
2016-07-21 06:45:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2018-05-14 06:56:16 +00:00
|
|
|
fmt.Printf("Failed to get stat for file %s: %v\n", fileOrDir, err)
|
2016-07-21 06:45:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
mode := fi.Mode()
|
|
|
|
if mode.IsDir() {
|
|
|
|
files, _ := ioutil.ReadDir(fileOrDir)
|
|
|
|
for _, subFileOrDir := range files {
|
2019-03-16 00:20:24 +00:00
|
|
|
if !doEachCopy(ctx, fileOrDir+"/"+subFileOrDir.Name(), filerAddress, filerGrpcAddress, grpcDialOption, path+fi.Name()+"/") {
|
2016-07-21 06:45:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a regular file
|
|
|
|
if *copy.include != "" {
|
|
|
|
if ok, _ := filepath.Match(*copy.include, filepath.Base(fileOrDir)); !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 06:46:45 +00:00
|
|
|
// find the chunk count
|
|
|
|
chunkSize := int64(*copy.maxMB * 1024 * 1024)
|
|
|
|
chunkCount := 1
|
|
|
|
if chunkSize > 0 && fi.Size() > chunkSize {
|
|
|
|
chunkCount = int(fi.Size()/chunkSize) + 1
|
|
|
|
}
|
|
|
|
|
2018-05-30 08:05:26 +00:00
|
|
|
if chunkCount == 1 {
|
2019-03-16 00:20:24 +00:00
|
|
|
return uploadFileAsOne(ctx, filerAddress, filerGrpcAddress, grpcDialOption, path, f, fi)
|
2018-05-30 08:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
return uploadFileInChunks(ctx, filerAddress, filerGrpcAddress, grpcDialOption, path, f, fi, chunkCount, chunkSize)
|
2018-05-30 08:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func uploadFileAsOne(ctx context.Context, filerAddress, filerGrpcAddress string, grpcDialOption grpc.DialOption, urlFolder string, f *os.File, fi os.FileInfo) bool {
|
2018-05-30 08:05:26 +00:00
|
|
|
|
2018-05-30 06:46:45 +00:00
|
|
|
// upload the file content
|
2018-05-31 03:48:13 +00:00
|
|
|
fileName := filepath.Base(f.Name())
|
2018-05-30 06:52:27 +00:00
|
|
|
mimeType := detectMimeType(f)
|
2018-05-30 06:46:45 +00:00
|
|
|
|
2018-05-31 05:28:14 +00:00
|
|
|
var chunks []*filer_pb.FileChunk
|
2016-07-21 06:45:55 +00:00
|
|
|
|
2018-05-31 05:28:14 +00:00
|
|
|
if fi.Size() > 0 {
|
|
|
|
|
|
|
|
// assign a volume
|
2019-02-18 20:11:52 +00:00
|
|
|
assignResult, err := operation.Assign(*copy.master, grpcDialOption, &operation.VolumeAssignRequest{
|
2018-05-31 05:28:14 +00:00
|
|
|
Count: 1,
|
|
|
|
Replication: *copy.replication,
|
|
|
|
Collection: *copy.collection,
|
|
|
|
Ttl: *copy.ttl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to assign from %s: %v\n", *copy.master, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
|
|
|
|
|
2019-02-15 08:09:19 +00:00
|
|
|
uploadResult, err := operation.Upload(targetUrl, fileName, f, false, mimeType, nil, assignResult.Auth)
|
2018-05-31 05:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("upload data %v to %s: %v\n", fileName, targetUrl, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if uploadResult.Error != "" {
|
|
|
|
fmt.Printf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fmt.Printf("uploaded %s to %s\n", fileName, targetUrl)
|
|
|
|
|
|
|
|
chunks = append(chunks, &filer_pb.FileChunk{
|
|
|
|
FileId: assignResult.Fid,
|
|
|
|
Offset: 0,
|
|
|
|
Size: uint64(uploadResult.Size),
|
|
|
|
Mtime: time.Now().UnixNano(),
|
2018-09-23 05:12:21 +00:00
|
|
|
ETag: uploadResult.ETag,
|
2018-05-31 05:28:14 +00:00
|
|
|
})
|
|
|
|
|
2018-06-06 06:37:41 +00:00
|
|
|
fmt.Printf("copied %s => http://%s%s%s\n", fileName, filerAddress, urlFolder, fileName)
|
2016-07-21 06:45:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
if err := withFilerClient(ctx, filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-31 05:28:14 +00:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: urlFolder,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: fileName,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
2018-06-10 23:57:32 +00:00
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Gid: uint32(os.Getgid()),
|
|
|
|
Uid: uint32(os.Getuid()),
|
|
|
|
FileSize: uint64(fi.Size()),
|
|
|
|
FileMode: uint32(fi.Mode()),
|
|
|
|
Mime: mimeType,
|
|
|
|
Replication: *copy.replication,
|
|
|
|
Collection: *copy.collection,
|
|
|
|
TtlSec: int32(util.ParseInt(*copy.ttl, 0)),
|
2018-05-31 05:28:14 +00:00
|
|
|
},
|
|
|
|
Chunks: chunks,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-05-31 05:28:14 +00:00
|
|
|
return fmt.Errorf("update fh: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2018-06-06 06:37:41 +00:00
|
|
|
fmt.Printf("upload data %v to http://%s%s%s: %v\n", fileName, filerAddress, urlFolder, fileName, err)
|
2016-07-21 06:45:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2018-05-30 06:46:45 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func uploadFileInChunks(ctx context.Context, filerAddress, filerGrpcAddress string, grpcDialOption grpc.DialOption, urlFolder string, f *os.File, fi os.FileInfo, chunkCount int, chunkSize int64) bool {
|
2018-05-30 07:54:56 +00:00
|
|
|
|
2018-05-31 03:48:13 +00:00
|
|
|
fileName := filepath.Base(f.Name())
|
2018-05-31 03:24:57 +00:00
|
|
|
mimeType := detectMimeType(f)
|
|
|
|
|
2018-05-30 07:54:56 +00:00
|
|
|
var chunks []*filer_pb.FileChunk
|
|
|
|
|
|
|
|
for i := int64(0); i < int64(chunkCount); i++ {
|
2018-05-30 08:05:26 +00:00
|
|
|
|
|
|
|
// assign a volume
|
2019-02-18 20:11:52 +00:00
|
|
|
assignResult, err := operation.Assign(*copy.master, grpcDialOption, &operation.VolumeAssignRequest{
|
2018-05-30 08:05:26 +00:00
|
|
|
Count: 1,
|
|
|
|
Replication: *copy.replication,
|
|
|
|
Collection: *copy.collection,
|
|
|
|
Ttl: *copy.ttl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to assign from %s: %v\n", *copy.master, err)
|
2018-05-30 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 08:05:26 +00:00
|
|
|
targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
|
2018-05-30 07:54:56 +00:00
|
|
|
|
|
|
|
uploadResult, err := operation.Upload(targetUrl,
|
2018-05-31 03:48:13 +00:00
|
|
|
fileName+"-"+strconv.FormatInt(i+1, 10),
|
2018-05-30 07:54:56 +00:00
|
|
|
io.LimitReader(f, chunkSize),
|
2019-02-15 08:09:19 +00:00
|
|
|
false, "application/octet-stream", nil, assignResult.Auth)
|
2018-05-30 07:54:56 +00:00
|
|
|
if err != nil {
|
2018-05-31 03:48:13 +00:00
|
|
|
fmt.Printf("upload data %v to %s: %v\n", fileName, targetUrl, err)
|
2018-05-30 07:54:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if uploadResult.Error != "" {
|
2018-05-31 03:48:13 +00:00
|
|
|
fmt.Printf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
|
2018-05-30 07:54:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
chunks = append(chunks, &filer_pb.FileChunk{
|
2018-05-30 08:05:26 +00:00
|
|
|
FileId: assignResult.Fid,
|
2018-05-30 07:54:56 +00:00
|
|
|
Offset: i * chunkSize,
|
|
|
|
Size: uint64(uploadResult.Size),
|
|
|
|
Mtime: time.Now().UnixNano(),
|
2018-09-23 05:12:21 +00:00
|
|
|
ETag: uploadResult.ETag,
|
2018-05-30 07:54:56 +00:00
|
|
|
})
|
2018-05-31 03:48:13 +00:00
|
|
|
fmt.Printf("uploaded %s-%d to %s [%d,%d)\n", fileName, i+1, targetUrl, i*chunkSize, i*chunkSize+int64(uploadResult.Size))
|
2018-05-30 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
if err := withFilerClient(ctx, filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-30 07:54:56 +00:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: urlFolder,
|
|
|
|
Entry: &filer_pb.Entry{
|
2018-05-31 03:48:13 +00:00
|
|
|
Name: fileName,
|
2018-05-30 07:54:56 +00:00
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
2018-06-10 23:57:32 +00:00
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Gid: uint32(os.Getgid()),
|
|
|
|
Uid: uint32(os.Getuid()),
|
|
|
|
FileSize: uint64(fi.Size()),
|
|
|
|
FileMode: uint32(fi.Mode()),
|
|
|
|
Mime: mimeType,
|
|
|
|
Replication: *copy.replication,
|
|
|
|
Collection: *copy.collection,
|
|
|
|
TtlSec: int32(util.ParseInt(*copy.ttl, 0)),
|
2018-05-30 07:54:56 +00:00
|
|
|
},
|
|
|
|
Chunks: chunks,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-05-30 07:54:56 +00:00
|
|
|
return fmt.Errorf("update fh: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2018-06-06 06:37:41 +00:00
|
|
|
fmt.Printf("upload data %v to http://%s%s%s: %v\n", fileName, filerAddress, urlFolder, fileName, err)
|
2018-05-30 07:54:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-06-06 06:37:41 +00:00
|
|
|
fmt.Printf("copied %s => http://%s%s%s\n", fileName, filerAddress, urlFolder, fileName)
|
2018-05-30 08:05:26 +00:00
|
|
|
|
2018-05-30 07:54:56 +00:00
|
|
|
return true
|
2018-05-30 06:46:45 +00:00
|
|
|
}
|
2018-05-30 06:52:27 +00:00
|
|
|
|
|
|
|
func detectMimeType(f *os.File) string {
|
|
|
|
head := make([]byte, 512)
|
2019-01-17 01:17:19 +00:00
|
|
|
f.Seek(0, io.SeekStart)
|
2018-05-30 06:52:27 +00:00
|
|
|
n, err := f.Read(head)
|
2018-05-31 05:28:14 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return ""
|
|
|
|
}
|
2018-05-30 06:52:27 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("read head of %v: %v\n", f.Name(), err)
|
|
|
|
return "application/octet-stream"
|
|
|
|
}
|
2019-01-17 01:17:19 +00:00
|
|
|
f.Seek(0, io.SeekStart)
|
2018-05-30 06:52:27 +00:00
|
|
|
mimeType := http.DetectContentType(head[:n])
|
|
|
|
return mimeType
|
|
|
|
}
|
2018-05-30 07:54:56 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func withFilerClient(ctx context.Context, filerAddress string, grpcDialOption grpc.DialOption, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2018-05-30 07:54:56 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
grpcConnection, err := util.GrpcDial(ctx, filerAddress, grpcDialOption)
|
2018-05-30 07:54:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to dial %s: %v", filerAddress, err)
|
|
|
|
}
|
|
|
|
defer grpcConnection.Close()
|
|
|
|
|
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
|
|
|
|
|
|
|
return fn(client)
|
|
|
|
}
|