weedfs_write.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package mount
  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(false, func(client filer_pb.SeaweedFilerClient) error {
  18. return util.Retry("assignVolume", func() error {
  19. request := &filer_pb.AssignVolumeRequest{
  20. Count: 1,
  21. Replication: wfs.option.Replication,
  22. Collection: wfs.option.Collection,
  23. TtlSec: wfs.option.TtlSec,
  24. DiskType: string(wfs.option.DiskType),
  25. DataCenter: wfs.option.DataCenter,
  26. Path: string(fullPath),
  27. }
  28. resp, err := client.AssignVolume(context.Background(), request)
  29. if err != nil {
  30. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  31. return err
  32. }
  33. if resp.Error != "" {
  34. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  35. }
  36. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  37. loc := resp.Location
  38. host = wfs.AdjustedUrl(loc)
  39. collection, replication = resp.Collection, resp.Replication
  40. return nil
  41. })
  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.VolumeServerAccess == "filerProxy" {
  47. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.getCurrentFiler(), fileId)
  48. }
  49. uploadOption := &operation.UploadOption{
  50. UploadUrl: fileUrl,
  51. Filename: filename,
  52. Cipher: wfs.option.Cipher,
  53. IsInputCompressed: false,
  54. MimeType: "",
  55. PairMap: nil,
  56. Jwt: auth,
  57. }
  58. uploadResult, err, data := operation.Upload(reader, uploadOption)
  59. if err != nil {
  60. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  61. return nil, "", "", fmt.Errorf("upload data: %v", err)
  62. }
  63. if uploadResult.Error != "" {
  64. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  65. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  66. }
  67. if offset == 0 {
  68. wfs.chunkCache.SetChunk(fileId, data)
  69. }
  70. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  71. return chunk, collection, replication, nil
  72. }
  73. }