wfs_write.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = wfs.AdjustedUrl(loc)
  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. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  47. if err != nil {
  48. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  49. return nil, "", "", fmt.Errorf("upload data: %v", err)
  50. }
  51. if uploadResult.Error != "" {
  52. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  53. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  54. }
  55. wfs.chunkCache.SetChunk(fileId, data)
  56. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  57. return chunk, collection, replication, nil
  58. }
  59. }