You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

3 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. )
  11. func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {
  12. resp = &volume_server_pb.FetchAndWriteNeedleResponse{}
  13. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  14. if v == nil {
  15. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  16. }
  17. remoteConf := &filer_pb.RemoteConf{
  18. Type: req.RemoteType,
  19. Name: req.RemoteName,
  20. S3AccessKey: req.S3AccessKey,
  21. S3SecretKey: req.S3SecretKey,
  22. S3Region: req.S3Region,
  23. S3Endpoint: req.S3Endpoint,
  24. }
  25. client, getClientErr := remote_storage.GetRemoteStorage(remoteConf)
  26. if getClientErr != nil {
  27. return nil, fmt.Errorf("get remote client: %v", getClientErr)
  28. }
  29. remoteStorageLocation := &filer_pb.RemoteStorageLocation{
  30. Name: req.RemoteName,
  31. Bucket: req.RemoteBucket,
  32. Path: req.RemoteKey,
  33. }
  34. data, ReadRemoteErr := client.ReadFile(remoteStorageLocation, req.Offset, req.Size)
  35. if ReadRemoteErr != nil {
  36. return nil, fmt.Errorf("read from remote %+v: %v", remoteStorageLocation, ReadRemoteErr)
  37. }
  38. if err = v.WriteNeedleBlob(types.NeedleId(req.NeedleId), data, types.Size(req.Size)); err != nil {
  39. return nil, fmt.Errorf("write blob needle %d size %d: %v", req.NeedleId, req.Size, err)
  40. }
  41. return resp, nil
  42. }