wfs_write.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. )
  12. func (wfs *WFS) saveDataAsChunk(dir string) filer2.SaveDataAsChunkFunctionType {
  13. return func(reader io.Reader, filename string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error) {
  14. var fileId, host string
  15. var auth security.EncodedJwt
  16. if err := wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  17. request := &filer_pb.AssignVolumeRequest{
  18. Count: 1,
  19. Replication: wfs.option.Replication,
  20. Collection: wfs.option.Collection,
  21. TtlSec: wfs.option.TtlSec,
  22. DataCenter: wfs.option.DataCenter,
  23. ParentPath: dir,
  24. }
  25. resp, err := client.AssignVolume(context.Background(), request)
  26. if err != nil {
  27. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  28. return err
  29. }
  30. if resp.Error != "" {
  31. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  32. }
  33. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  34. host = wfs.AdjustedUrl(host)
  35. collection, replication = resp.Collection, resp.Replication
  36. return nil
  37. }); err != nil {
  38. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  39. }
  40. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  41. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  42. if err != nil {
  43. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  44. return nil, "", "", fmt.Errorf("upload data: %v", err)
  45. }
  46. if uploadResult.Error != "" {
  47. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  48. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  49. }
  50. wfs.chunkCache.SetChunk(fileId, data)
  51. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  52. return chunk, "", "", nil
  53. }
  54. }