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.

656 lines
20 KiB

5 years ago
4 years ago
7 years ago
4 years ago
5 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "google.golang.org/grpc"
  16. "github.com/chrislusf/seaweedfs/weed/util/grace"
  17. "github.com/chrislusf/seaweedfs/weed/operation"
  18. "github.com/chrislusf/seaweedfs/weed/pb"
  19. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  20. "github.com/chrislusf/seaweedfs/weed/security"
  21. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  22. "github.com/chrislusf/seaweedfs/weed/util"
  23. "github.com/chrislusf/seaweedfs/weed/wdclient"
  24. )
  25. var (
  26. copy CopyOptions
  27. waitGroup sync.WaitGroup
  28. )
  29. type CopyOptions struct {
  30. include *string
  31. replication *string
  32. collection *string
  33. ttl *string
  34. diskType *string
  35. maxMB *int
  36. masterClient *wdclient.MasterClient
  37. concurrenctFiles *int
  38. concurrenctChunks *int
  39. grpcDialOption grpc.DialOption
  40. masters []string
  41. cipher bool
  42. ttlSec int32
  43. checkSize *bool
  44. verbose *bool
  45. }
  46. func init() {
  47. cmdFilerCopy.Run = runCopy // break init cycle
  48. cmdFilerCopy.IsDebug = cmdFilerCopy.Flag.Bool("debug", false, "verbose debug information")
  49. copy.include = cmdFilerCopy.Flag.String("include", "", "pattens of files to copy, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
  50. copy.replication = cmdFilerCopy.Flag.String("replication", "", "replication type")
  51. copy.collection = cmdFilerCopy.Flag.String("collection", "", "optional collection name")
  52. copy.ttl = cmdFilerCopy.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
  53. copy.diskType = cmdFilerCopy.Flag.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  54. copy.maxMB = cmdFilerCopy.Flag.Int("maxMB", 4, "split files larger than the limit")
  55. copy.concurrenctFiles = cmdFilerCopy.Flag.Int("c", 8, "concurrent file copy goroutines")
  56. copy.concurrenctChunks = cmdFilerCopy.Flag.Int("concurrentChunks", 8, "concurrent chunk copy goroutines for each file")
  57. copy.checkSize = cmdFilerCopy.Flag.Bool("check.size", false, "copy when the target file size is different from the source file")
  58. copy.verbose = cmdFilerCopy.Flag.Bool("verbose", false, "print out details during copying")
  59. }
  60. var cmdFilerCopy = &Command{
  61. UsageLine: "filer.copy file_or_dir1 [file_or_dir2 file_or_dir3] http://localhost:8888/path/to/a/folder/",
  62. Short: "copy one or a list of files to a filer folder",
  63. Long: `copy one or a list of files, or batch copy one whole folder recursively, to a filer folder
  64. It can copy one or a list of files or folders.
  65. If copying a whole folder recursively:
  66. All files under the folder and subfolders will be copyed.
  67. Optional parameter "-include" allows you to specify the file name patterns.
  68. If "maxMB" is set to a positive number, files larger than it would be split into chunks.
  69. `,
  70. }
  71. func runCopy(cmd *Command, args []string) bool {
  72. util.LoadConfiguration("security", false)
  73. if len(args) <= 1 {
  74. return false
  75. }
  76. filerDestination := args[len(args)-1]
  77. fileOrDirs := args[0 : len(args)-1]
  78. filerAddress, urlPath, err := pb.ParseUrl(filerDestination)
  79. if err != nil {
  80. fmt.Printf("The last argument should be a URL on filer: %v\n", err)
  81. return false
  82. }
  83. if !strings.HasSuffix(urlPath, "/") {
  84. fmt.Printf("The last argument should be a folder and end with \"/\"\n")
  85. return false
  86. }
  87. copy.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  88. masters, collection, replication, dirBuckets, maxMB, cipher, err := readFilerConfiguration(copy.grpcDialOption, filerAddress)
  89. if err != nil {
  90. fmt.Printf("read from filer %s: %v\n", filerAddress, err)
  91. return false
  92. }
  93. if strings.HasPrefix(urlPath, dirBuckets+"/") {
  94. restPath := urlPath[len(dirBuckets)+1:]
  95. if strings.Index(restPath, "/") > 0 {
  96. expectedBucket := restPath[:strings.Index(restPath, "/")]
  97. if *copy.collection == "" {
  98. *copy.collection = expectedBucket
  99. } else if *copy.collection != expectedBucket {
  100. fmt.Printf("destination %s uses collection \"%s\": unexpected collection \"%v\"\n", urlPath, expectedBucket, *copy.collection)
  101. return true
  102. }
  103. }
  104. }
  105. if *copy.collection == "" {
  106. *copy.collection = collection
  107. }
  108. if *copy.replication == "" {
  109. *copy.replication = replication
  110. }
  111. if *copy.maxMB == 0 {
  112. *copy.maxMB = int(maxMB)
  113. }
  114. copy.masters = masters
  115. copy.cipher = cipher
  116. ttl, err := needle.ReadTTL(*copy.ttl)
  117. if err != nil {
  118. fmt.Printf("parsing ttl %s: %v\n", *copy.ttl, err)
  119. return false
  120. }
  121. copy.ttlSec = int32(ttl.Minutes()) * 60
  122. if *cmdFilerCopy.IsDebug {
  123. grace.SetupProfiling("filer.copy.cpu.pprof", "filer.copy.mem.pprof")
  124. }
  125. fileCopyTaskChan := make(chan FileCopyTask, *copy.concurrenctFiles)
  126. go func() {
  127. defer close(fileCopyTaskChan)
  128. for _, fileOrDir := range fileOrDirs {
  129. if err := genFileCopyTask(fileOrDir, urlPath, fileCopyTaskChan); err != nil {
  130. fmt.Fprintf(os.Stderr, "genFileCopyTask : %v\n", err)
  131. break
  132. }
  133. }
  134. }()
  135. for i := 0; i < *copy.concurrenctFiles; i++ {
  136. waitGroup.Add(1)
  137. go func() {
  138. defer waitGroup.Done()
  139. worker := FileCopyWorker{
  140. options: &copy,
  141. filerAddress: filerAddress,
  142. }
  143. if err := worker.copyFiles(fileCopyTaskChan); err != nil {
  144. fmt.Fprintf(os.Stderr, "copy file error: %v\n", err)
  145. return
  146. }
  147. }()
  148. }
  149. waitGroup.Wait()
  150. return true
  151. }
  152. func readFilerConfiguration(grpcDialOption grpc.DialOption, filerGrpcAddress pb.ServerAddress) (masters []string, collection, replication string, dirBuckets string, maxMB uint32, cipher bool, err error) {
  153. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  154. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  155. if err != nil {
  156. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  157. }
  158. masters, collection, replication, maxMB = resp.Masters, resp.Collection, resp.Replication, resp.MaxMb
  159. dirBuckets = resp.DirBuckets
  160. cipher = resp.Cipher
  161. return nil
  162. })
  163. return
  164. }
  165. func genFileCopyTask(fileOrDir string, destPath string, fileCopyTaskChan chan FileCopyTask) error {
  166. fi, err := os.Stat(fileOrDir)
  167. if err != nil {
  168. fmt.Fprintf(os.Stderr, "Error: read file %s: %v\n", fileOrDir, err)
  169. return nil
  170. }
  171. mode := fi.Mode()
  172. uid, gid := util.GetFileUidGid(fi)
  173. fileSize := fi.Size()
  174. if mode.IsDir() {
  175. fileSize = 0
  176. }
  177. fileCopyTaskChan <- FileCopyTask{
  178. sourceLocation: fileOrDir,
  179. destinationUrlPath: destPath,
  180. fileSize: fileSize,
  181. fileMode: fi.Mode(),
  182. uid: uid,
  183. gid: gid,
  184. }
  185. if mode.IsDir() {
  186. files, _ := ioutil.ReadDir(fileOrDir)
  187. for _, subFileOrDir := range files {
  188. cleanedDestDirectory := filepath.Clean(destPath + fi.Name())
  189. if err = genFileCopyTask(fileOrDir+"/"+subFileOrDir.Name(), cleanedDestDirectory+"/", fileCopyTaskChan); err != nil {
  190. return err
  191. }
  192. }
  193. }
  194. return nil
  195. }
  196. type FileCopyWorker struct {
  197. options *CopyOptions
  198. filerAddress pb.ServerAddress
  199. }
  200. func (worker *FileCopyWorker) copyFiles(fileCopyTaskChan chan FileCopyTask) error {
  201. for task := range fileCopyTaskChan {
  202. if err := worker.doEachCopy(task); err != nil {
  203. return err
  204. }
  205. }
  206. return nil
  207. }
  208. type FileCopyTask struct {
  209. sourceLocation string
  210. destinationUrlPath string
  211. fileSize int64
  212. fileMode os.FileMode
  213. uid uint32
  214. gid uint32
  215. }
  216. func (worker *FileCopyWorker) doEachCopy(task FileCopyTask) error {
  217. f, err := os.Open(task.sourceLocation)
  218. if err != nil {
  219. fmt.Printf("Failed to open file %s: %v\n", task.sourceLocation, err)
  220. if _, ok := err.(*os.PathError); ok {
  221. fmt.Printf("skipping %s\n", task.sourceLocation)
  222. return nil
  223. }
  224. return err
  225. }
  226. defer f.Close()
  227. // this is a regular file
  228. if *worker.options.include != "" {
  229. if ok, _ := filepath.Match(*worker.options.include, filepath.Base(task.sourceLocation)); !ok {
  230. return nil
  231. }
  232. }
  233. if shouldCopy, err := worker.checkExistingFileFirst(task, f); err != nil {
  234. return fmt.Errorf("check existing file: %v", err)
  235. } else if !shouldCopy {
  236. if *worker.options.verbose {
  237. fmt.Printf("skipping copied file: %v\n", f.Name())
  238. }
  239. return nil
  240. }
  241. // find the chunk count
  242. chunkSize := int64(*worker.options.maxMB * 1024 * 1024)
  243. chunkCount := 1
  244. if chunkSize > 0 && task.fileSize > chunkSize {
  245. chunkCount = int(task.fileSize/chunkSize) + 1
  246. }
  247. if chunkCount == 1 {
  248. return worker.uploadFileAsOne(task, f)
  249. }
  250. return worker.uploadFileInChunks(task, f, chunkCount, chunkSize)
  251. }
  252. func (worker *FileCopyWorker) checkExistingFileFirst(task FileCopyTask, f *os.File) (shouldCopy bool, err error) {
  253. shouldCopy = true
  254. if !*worker.options.checkSize {
  255. return
  256. }
  257. fileStat, err := f.Stat()
  258. if err != nil {
  259. shouldCopy = false
  260. return
  261. }
  262. err = pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  263. request := &filer_pb.LookupDirectoryEntryRequest{
  264. Directory: task.destinationUrlPath,
  265. Name: filepath.Base(f.Name()),
  266. }
  267. resp, lookupErr := client.LookupDirectoryEntry(context.Background(), request)
  268. if lookupErr != nil {
  269. // mostly not found error
  270. return nil
  271. }
  272. if fileStat.Size() == int64(filer.FileSize(resp.Entry)) {
  273. shouldCopy = false
  274. }
  275. return nil
  276. })
  277. return
  278. }
  279. func (worker *FileCopyWorker) uploadFileAsOne(task FileCopyTask, f *os.File) error {
  280. // upload the file content
  281. fileName := filepath.Base(f.Name())
  282. var mimeType string
  283. var chunks []*filer_pb.FileChunk
  284. var assignResult *filer_pb.AssignVolumeResponse
  285. var assignError error
  286. if task.fileMode&os.ModeDir == 0 && task.fileSize > 0 {
  287. mimeType = detectMimeType(f)
  288. data, err := ioutil.ReadAll(f)
  289. if err != nil {
  290. return err
  291. }
  292. err = util.Retry("upload", func() error {
  293. // assign a volume
  294. assignErr := pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  295. request := &filer_pb.AssignVolumeRequest{
  296. Count: 1,
  297. Replication: *worker.options.replication,
  298. Collection: *worker.options.collection,
  299. TtlSec: worker.options.ttlSec,
  300. DiskType: *worker.options.diskType,
  301. Path: task.destinationUrlPath,
  302. }
  303. assignResult, assignError = client.AssignVolume(context.Background(), request)
  304. if assignError != nil {
  305. return fmt.Errorf("assign volume failure %v: %v", request, assignError)
  306. }
  307. if assignResult.Error != "" {
  308. return fmt.Errorf("assign volume failure %v: %v", request, assignResult.Error)
  309. }
  310. if assignResult.Location.Url == "" {
  311. return fmt.Errorf("assign volume failure %v: %v", request, assignResult)
  312. }
  313. return nil
  314. })
  315. if assignErr != nil {
  316. return assignErr
  317. }
  318. // upload data
  319. targetUrl := "http://" + assignResult.Location.Url + "/" + assignResult.FileId
  320. uploadOption := &operation.UploadOption{
  321. UploadUrl: targetUrl,
  322. Filename: fileName,
  323. Cipher: worker.options.cipher,
  324. IsInputCompressed: false,
  325. MimeType: mimeType,
  326. PairMap: nil,
  327. Jwt: security.EncodedJwt(assignResult.Auth),
  328. }
  329. uploadResult, err := operation.UploadData(data, uploadOption)
  330. if err != nil {
  331. return fmt.Errorf("upload data %v to %s: %v\n", fileName, targetUrl, err)
  332. }
  333. if uploadResult.Error != "" {
  334. return fmt.Errorf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
  335. }
  336. if *worker.options.verbose {
  337. fmt.Printf("uploaded %s to %s\n", fileName, targetUrl)
  338. }
  339. fmt.Printf("copied %s => http://%s%s%s\n", f.Name(), worker.filerAddress.ToHttpAddress(), task.destinationUrlPath, fileName)
  340. chunks = append(chunks, uploadResult.ToPbFileChunk(assignResult.FileId, 0))
  341. return nil
  342. })
  343. if err != nil {
  344. return fmt.Errorf("upload %v: %v\n", fileName, err)
  345. }
  346. }
  347. if err := pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  348. request := &filer_pb.CreateEntryRequest{
  349. Directory: task.destinationUrlPath,
  350. Entry: &filer_pb.Entry{
  351. Name: fileName,
  352. Attributes: &filer_pb.FuseAttributes{
  353. Crtime: time.Now().Unix(),
  354. Mtime: time.Now().Unix(),
  355. Gid: task.gid,
  356. Uid: task.uid,
  357. FileSize: uint64(task.fileSize),
  358. FileMode: uint32(task.fileMode),
  359. Mime: mimeType,
  360. Replication: *worker.options.replication,
  361. Collection: *worker.options.collection,
  362. TtlSec: worker.options.ttlSec,
  363. },
  364. Chunks: chunks,
  365. },
  366. }
  367. if err := filer_pb.CreateEntry(client, request); err != nil {
  368. return fmt.Errorf("update fh: %v", err)
  369. }
  370. return nil
  371. }); err != nil {
  372. return fmt.Errorf("upload data %v to http://%s%s%s: %v\n", fileName, worker.filerAddress.ToHttpAddress(), task.destinationUrlPath, fileName, err)
  373. }
  374. return nil
  375. }
  376. func (worker *FileCopyWorker) uploadFileInChunks(task FileCopyTask, f *os.File, chunkCount int, chunkSize int64) error {
  377. fileName := filepath.Base(f.Name())
  378. mimeType := detectMimeType(f)
  379. chunksChan := make(chan *filer_pb.FileChunk, chunkCount)
  380. concurrentChunks := make(chan struct{}, *worker.options.concurrenctChunks)
  381. var wg sync.WaitGroup
  382. var uploadError error
  383. var collection, replication string
  384. fmt.Printf("uploading %s in %d chunks ...\n", fileName, chunkCount)
  385. for i := int64(0); i < int64(chunkCount) && uploadError == nil; i++ {
  386. wg.Add(1)
  387. concurrentChunks <- struct{}{}
  388. go func(i int64) {
  389. defer func() {
  390. wg.Done()
  391. <-concurrentChunks
  392. }()
  393. // assign a volume
  394. var assignResult *filer_pb.AssignVolumeResponse
  395. var assignError error
  396. err := util.Retry("assignVolume", func() error {
  397. return pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  398. request := &filer_pb.AssignVolumeRequest{
  399. Count: 1,
  400. Replication: *worker.options.replication,
  401. Collection: *worker.options.collection,
  402. TtlSec: worker.options.ttlSec,
  403. DiskType: *worker.options.diskType,
  404. Path: task.destinationUrlPath + fileName,
  405. }
  406. assignResult, assignError = client.AssignVolume(context.Background(), request)
  407. if assignError != nil {
  408. return fmt.Errorf("assign volume failure %v: %v", request, assignError)
  409. }
  410. if assignResult.Error != "" {
  411. return fmt.Errorf("assign volume failure %v: %v", request, assignResult.Error)
  412. }
  413. return nil
  414. })
  415. })
  416. if err != nil {
  417. fmt.Printf("Failed to assign from %v: %v\n", worker.options.masters, err)
  418. }
  419. targetUrl := "http://" + assignResult.Location.Url + "/" + assignResult.FileId
  420. if collection == "" {
  421. collection = assignResult.Collection
  422. }
  423. if replication == "" {
  424. replication = assignResult.Replication
  425. }
  426. uploadOption := &operation.UploadOption{
  427. UploadUrl: targetUrl,
  428. Filename: fileName + "-" + strconv.FormatInt(i+1, 10),
  429. Cipher: worker.options.cipher,
  430. IsInputCompressed: false,
  431. MimeType: "",
  432. PairMap: nil,
  433. Jwt: security.EncodedJwt(assignResult.Auth),
  434. }
  435. uploadResult, err, _ := operation.Upload(io.NewSectionReader(f, i*chunkSize, chunkSize), uploadOption)
  436. if err != nil {
  437. uploadError = fmt.Errorf("upload data %v to %s: %v\n", fileName, targetUrl, err)
  438. return
  439. }
  440. if uploadResult.Error != "" {
  441. uploadError = fmt.Errorf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
  442. return
  443. }
  444. chunksChan <- uploadResult.ToPbFileChunk(assignResult.FileId, i*chunkSize)
  445. fmt.Printf("uploaded %s-%d to %s [%d,%d)\n", fileName, i+1, targetUrl, i*chunkSize, i*chunkSize+int64(uploadResult.Size))
  446. }(i)
  447. }
  448. wg.Wait()
  449. close(chunksChan)
  450. var chunks []*filer_pb.FileChunk
  451. for chunk := range chunksChan {
  452. chunks = append(chunks, chunk)
  453. }
  454. if uploadError != nil {
  455. var fileIds []string
  456. for _, chunk := range chunks {
  457. fileIds = append(fileIds, chunk.FileId)
  458. }
  459. operation.DeleteFiles(func() pb.ServerAddress {
  460. return pb.ServerAddress(copy.masters[0])
  461. }, false, worker.options.grpcDialOption, fileIds)
  462. return uploadError
  463. }
  464. manifestedChunks, manifestErr := filer.MaybeManifestize(worker.saveDataAsChunk, chunks)
  465. if manifestErr != nil {
  466. return fmt.Errorf("create manifest: %v", manifestErr)
  467. }
  468. if err := pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  469. request := &filer_pb.CreateEntryRequest{
  470. Directory: task.destinationUrlPath,
  471. Entry: &filer_pb.Entry{
  472. Name: fileName,
  473. Attributes: &filer_pb.FuseAttributes{
  474. Crtime: time.Now().Unix(),
  475. Mtime: time.Now().Unix(),
  476. Gid: task.gid,
  477. Uid: task.uid,
  478. FileSize: uint64(task.fileSize),
  479. FileMode: uint32(task.fileMode),
  480. Mime: mimeType,
  481. Replication: replication,
  482. Collection: collection,
  483. TtlSec: worker.options.ttlSec,
  484. },
  485. Chunks: manifestedChunks,
  486. },
  487. }
  488. if err := filer_pb.CreateEntry(client, request); err != nil {
  489. return fmt.Errorf("update fh: %v", err)
  490. }
  491. return nil
  492. }); err != nil {
  493. return fmt.Errorf("upload data %v to http://%s%s%s: %v\n", fileName, worker.filerAddress.ToHttpAddress(), task.destinationUrlPath, fileName, err)
  494. }
  495. fmt.Printf("copied %s => http://%s%s%s\n", f.Name(), worker.filerAddress.ToHttpAddress(), task.destinationUrlPath, fileName)
  496. return nil
  497. }
  498. func detectMimeType(f *os.File) string {
  499. head := make([]byte, 512)
  500. f.Seek(0, io.SeekStart)
  501. n, err := f.Read(head)
  502. if err == io.EOF {
  503. return ""
  504. }
  505. if err != nil {
  506. fmt.Printf("read head of %v: %v\n", f.Name(), err)
  507. return ""
  508. }
  509. f.Seek(0, io.SeekStart)
  510. mimeType := http.DetectContentType(head[:n])
  511. if mimeType == "application/octet-stream" {
  512. return ""
  513. }
  514. return mimeType
  515. }
  516. func (worker *FileCopyWorker) saveDataAsChunk(reader io.Reader, name string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error) {
  517. var fileId, host string
  518. var auth security.EncodedJwt
  519. if flushErr := pb.WithGrpcFilerClient(worker.filerAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  520. ctx := context.Background()
  521. assignErr := util.Retry("assignVolume", func() error {
  522. request := &filer_pb.AssignVolumeRequest{
  523. Count: 1,
  524. Replication: *worker.options.replication,
  525. Collection: *worker.options.collection,
  526. TtlSec: worker.options.ttlSec,
  527. DiskType: *worker.options.diskType,
  528. Path: name,
  529. }
  530. resp, err := client.AssignVolume(ctx, request)
  531. if err != nil {
  532. return fmt.Errorf("assign volume failure %v: %v", request, err)
  533. }
  534. if resp.Error != "" {
  535. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  536. }
  537. fileId, host, auth = resp.FileId, resp.Location.Url, security.EncodedJwt(resp.Auth)
  538. collection, replication = resp.Collection, resp.Replication
  539. return nil
  540. })
  541. if assignErr != nil {
  542. return assignErr
  543. }
  544. return nil
  545. }); flushErr != nil {
  546. return nil, collection, replication, fmt.Errorf("filerGrpcAddress assign volume: %v", flushErr)
  547. }
  548. uploadOption := &operation.UploadOption{
  549. UploadUrl: fmt.Sprintf("http://%s/%s", host, fileId),
  550. Filename: name,
  551. Cipher: worker.options.cipher,
  552. IsInputCompressed: false,
  553. MimeType: "",
  554. PairMap: nil,
  555. Jwt: auth,
  556. }
  557. uploadResult, flushErr, _ := operation.Upload(reader, uploadOption)
  558. if flushErr != nil {
  559. return nil, collection, replication, fmt.Errorf("upload data: %v", flushErr)
  560. }
  561. if uploadResult.Error != "" {
  562. return nil, collection, replication, fmt.Errorf("upload result: %v", uploadResult.Error)
  563. }
  564. return uploadResult.ToPbFileChunk(fileId, offset), collection, replication, nil
  565. }