weedfs_write.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package mount
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/operation"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. )
  11. func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath) filer.SaveDataAsChunkFunctionType {
  12. return func(reader io.Reader, filename string, offset int64, tsNs int64) (chunk *filer_pb.FileChunk, err error) {
  13. uploader, err := operation.NewUploader()
  14. if err != nil {
  15. return
  16. }
  17. fileId, uploadResult, err, data := uploader.UploadWithRetry(
  18. wfs,
  19. &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. &operation.UploadOption{
  29. Filename: filename,
  30. Cipher: wfs.option.Cipher,
  31. IsInputCompressed: false,
  32. MimeType: "",
  33. PairMap: nil,
  34. },
  35. func(host, fileId string) string {
  36. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  37. if wfs.option.VolumeServerAccess == "filerProxy" {
  38. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.getCurrentFiler(), fileId)
  39. }
  40. return fileUrl
  41. },
  42. reader,
  43. )
  44. if err != nil {
  45. glog.V(0).Infof("upload data %v: %v", filename, err)
  46. return nil, fmt.Errorf("upload data: %v", err)
  47. }
  48. if uploadResult.Error != "" {
  49. glog.V(0).Infof("upload failure %v: %v", filename, err)
  50. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  51. }
  52. if offset == 0 {
  53. wfs.chunkCache.SetChunk(fileId, data)
  54. }
  55. chunk = uploadResult.ToPbFileChunk(fileId, offset, tsNs)
  56. return chunk, nil
  57. }
  58. }