package fs import ( "ZhenTuLocalPassiveAdapter/config" "ZhenTuLocalPassiveAdapter/dto" "ZhenTuLocalPassiveAdapter/logger" "ZhenTuLocalPassiveAdapter/util" "context" "fmt" "path" "sort" "time" "github.com/aliyun/aliyun-oss-go-sdk/oss" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.uber.org/zap" ) type AliOSSAdapter struct { StorageConfig config.StorageConfig ossClient *oss.Client } func (a *AliOSSAdapter) getClient() (*oss.Client, error) { if a.ossClient == nil { client, err := oss.New( a.StorageConfig.AliOSS.Endpoint, a.StorageConfig.AliOSS.AccessKeyId, a.StorageConfig.AliOSS.AccessKeySecret, ) if err != nil { return nil, fmt.Errorf("创建阿里云OSS客户端失败: %w", err) } a.ossClient = client } return a.ossClient, nil } func (a *AliOSSAdapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) { _, span := tracer.Start(ctx, "GetFileList_alioss") defer span.End() span.SetAttributes(attribute.String("path", dirPath)) span.SetAttributes(attribute.String("relativeDate", relDt.Format("2006-01-02"))) if a.StorageConfig.AliOSS.Bucket == "" { span.SetAttributes(attribute.String("error", "未配置阿里云OSS存储桶")) span.SetStatus(codes.Error, "未配置阿里云OSS存储桶") return nil, fmt.Errorf("未配置阿里云OSS存储桶") } cacheKey := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02")) fileListCache := getAliOssFileListCache() if cachedFiles, ok := fileListCache.Get(cacheKey); ok { logger.Debug("获取已缓存列表", zap.String("cacheKey", cacheKey)) span.SetAttributes(attribute.Bool("cache.hit", true)) span.SetAttributes(attribute.Int("file.count", len(cachedFiles))) span.SetStatus(codes.Ok, "文件读取成功") return cachedFiles, nil } fileList, hit, shared, err := fileListCache.GetOrLoad(cacheKey, func() ([]dto.File, error) { client, err := a.getClient() if err != nil { return nil, err } bucket, err := client.Bucket(a.StorageConfig.AliOSS.Bucket) if err != nil { return nil, fmt.Errorf("获取存储桶失败: %w", err) } var resultFiles []dto.File prefix := path.Join(a.StorageConfig.AliOSS.Prefix, dirPath) marker := "" for { lsRes, err := bucket.ListObjects( oss.Prefix(prefix), oss.Marker(marker), oss.MaxKeys(1000), ) if err != nil { return nil, fmt.Errorf("文件列表读取失败: %w", err) } for _, object := range lsRes.Objects { key := object.Key if !util.IsVideoFile(path.Base(key)) { continue } startTime, stopTime, err := util.ParseStartStopTime(path.Base(key), relDt) if err != nil { continue } if stopTime.IsZero() { stopTime = startTime } if startTime.Equal(stopTime) { stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration)) } // 生成预签名URL(有效期10分钟) signedURL, err := bucket.SignURL(key, oss.HTTPGet, 600) if err != nil { logger.Error("生成预签名URL失败", zap.Error(err)) continue } resultFiles = append(resultFiles, dto.File{ BasePath: a.StorageConfig.AliOSS.Bucket, Name: path.Base(key), Path: path.Dir(key), Url: signedURL, StartTime: startTime, EndTime: stopTime, }) } if !lsRes.IsTruncated { break } marker = lsRes.NextMarker } sort.Slice(resultFiles, func(i, j int) bool { return resultFiles[i].StartTime.Before(resultFiles[j].StartTime) }) return resultFiles, nil }) if err != nil { span.SetAttributes(attribute.String("error", err.Error())) span.SetStatus(codes.Error, "文件列表读取失败") return nil, err } span.SetAttributes(attribute.Bool("cache.shared", shared)) span.SetAttributes(attribute.Int("file.count", len(fileList))) span.SetStatus(codes.Ok, "文件读取成功") if !hit && !shared { logger.Debug("缓存文件列表", zap.String("cacheKey", cacheKey)) } return fileList, nil }