wfs_write.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. DiskType: string(wfs.option.DiskType),
  24. DataCenter: wfs.option.DataCenter,
  25. Path: string(fullPath),
  26. }
  27. resp, err := client.AssignVolume(context.Background(), request)
  28. if err != nil {
  29. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  30. return err
  31. }
  32. if resp.Error != "" {
  33. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  34. }
  35. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  36. loc := &filer_pb.Location{
  37. Url: resp.Url,
  38. PublicUrl: resp.PublicUrl,
  39. }
  40. host = wfs.AdjustedUrl(loc)
  41. collection, replication = resp.Collection, resp.Replication
  42. return nil
  43. }); err != nil {
  44. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  45. }
  46. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  47. if wfs.option.VolumeServerAccess == "filerProxy" {
  48. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.option.FilerAddress, fileId)
  49. }
  50. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  51. if err != nil {
  52. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  53. return nil, "", "", fmt.Errorf("upload data: %v", err)
  54. }
  55. if uploadResult.Error != "" {
  56. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  57. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  58. }
  59. wfs.chunkCache.SetChunk(fileId, data)
  60. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  61. return chunk, collection, replication, nil
  62. }
  63. }