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.

74 lines
2.3 KiB

  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath) filer.SaveDataAsChunkFunctionType {
  14. return func(reader io.Reader, filename string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error) {
  15. var fileId, host string
  16. var auth security.EncodedJwt
  17. if err := wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  18. request := &filer_pb.AssignVolumeRequest{
  19. Count: 1,
  20. Replication: wfs.option.Replication,
  21. Collection: wfs.option.Collection,
  22. TtlSec: wfs.option.TtlSec,
  23. DataCenter: wfs.option.DataCenter,
  24. Path: string(fullPath),
  25. }
  26. resp, err := client.AssignVolume(context.Background(), request)
  27. if err != nil {
  28. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  29. return err
  30. }
  31. if resp.Error != "" {
  32. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  33. }
  34. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  35. loc := &filer_pb.Location{
  36. Url: resp.Url,
  37. PublicUrl: resp.PublicUrl,
  38. }
  39. host = loc.Url
  40. collection, replication = resp.Collection, resp.Replication
  41. return nil
  42. }); err != nil {
  43. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  44. }
  45. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  46. if wfs.option.OutsideContainerClusterMode {
  47. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.option.FilerAddress, fileId)
  48. }
  49. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  50. if err != nil {
  51. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  52. return nil, "", "", fmt.Errorf("upload data: %v", err)
  53. }
  54. if uploadResult.Error != "" {
  55. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  56. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  57. }
  58. wfs.chunkCache.SetChunk(fileId, data)
  59. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  60. return chunk, collection, replication, nil
  61. }
  62. }