faultRecordRepo.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package minio
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "path"
  8. "strings"
  9. minioUtil "fwh-toolbox-server/utils/minio"
  10. )
  11. const faultRecordPrefix = "fault-record/"
  12. // FaultRecordObjectKey fault-record/<env>/<gatewaySn>/<fileName>
  13. func FaultRecordObjectKey(env, gatewaySn, fileName string) (string, error) {
  14. for _, s := range []string{env, gatewaySn, fileName} {
  15. if s == "" || strings.ContainsAny(s, "/\\") || s == "." || s == ".." {
  16. return "", fmt.Errorf("invalid object key segment %q", s)
  17. }
  18. }
  19. return faultRecordPrefix + path.Join(env, gatewaySn, fileName), nil
  20. }
  21. // PutFaultRecordFile 保存下载到的原始录波压缩包
  22. func PutFaultRecordFile(ctx context.Context, key string, data []byte) error {
  23. return minioUtil.UploadFromReader(bytes.NewReader(data), int64(len(data)), key, "application/octet-stream")
  24. }
  25. // ReadFaultRecordFile 读取原始录波压缩包
  26. func ReadFaultRecordFile(ctx context.Context, key string) ([]byte, error) {
  27. obj, err := minioUtil.GetObject(key)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer obj.Close()
  32. return io.ReadAll(obj)
  33. }