You've already forked VptPassiveAdapter
s3 adapter
This commit is contained in:
76
fs/s3_adapter.go
Normal file
76
fs/s3_adapter.go
Normal file
@ -0,0 +1,76 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"ZhenTuLocalPassiveAdapter/config"
|
||||
"ZhenTuLocalPassiveAdapter/dto"
|
||||
"ZhenTuLocalPassiveAdapter/util"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"path"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awsConfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type S3Adapter struct {
|
||||
StorageConfig config.StorageConfig
|
||||
s3Client *s3.Client
|
||||
}
|
||||
|
||||
func (s *S3Adapter) getClient() (*s3.Client, error) {
|
||||
if s.s3Client == nil {
|
||||
creds := credentials.NewStaticCredentialsProvider(s.StorageConfig.S3.AkId, s.StorageConfig.S3.AkSec, "")
|
||||
cfg, err := awsConfig.LoadDefaultConfig(context.TODO(), awsConfig.WithCredentialsProvider(creds))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.s3Client = s3.NewFromConfig(cfg)
|
||||
}
|
||||
return s.s3Client, nil
|
||||
}
|
||||
|
||||
func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, error) {
|
||||
if s.StorageConfig.S3.Bucket == "" {
|
||||
return nil, fmt.Errorf("未配置S3存储桶")
|
||||
}
|
||||
|
||||
listObjectsInput := &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(s.StorageConfig.S3.Bucket),
|
||||
Prefix: aws.String(path.Join(s.StorageConfig.S3.Prefix, dirPath)),
|
||||
}
|
||||
|
||||
result, err := s.s3Client.ListObjectsV2(context.TODO(), listObjectsInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var fileList []dto.File
|
||||
for _, object := range result.Contents {
|
||||
key := *object.Key
|
||||
if util.IsVideoFile(path.Base(key)) {
|
||||
startTime, stopTime, err := util.ParseStartStopTime(path.Base(key), relDt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if startTime.Equal(stopTime) || stopTime.IsZero() {
|
||||
stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration))
|
||||
}
|
||||
fileList = append(fileList, dto.File{
|
||||
BasePath: s.StorageConfig.S3.Bucket,
|
||||
Name: path.Base(key),
|
||||
Path: path.Dir(key),
|
||||
Url: key,
|
||||
StartTime: startTime,
|
||||
EndTime: stopTime,
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.Slice(fileList, func(i, j int) bool {
|
||||
return fileList[i].StartTime.Before(fileList[j].StartTime)
|
||||
})
|
||||
return fileList, nil
|
||||
}
|
Reference in New Issue
Block a user