添加endpoint,测试s3

This commit is contained in:
2025-02-28 10:21:10 +08:00
parent 9bf07d3127
commit f22753aed6
5 changed files with 58 additions and 33 deletions

View File

@ -12,7 +12,6 @@ import (
"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"
)
@ -23,10 +22,20 @@ type S3Adapter struct {
func (s *S3Adapter) getClient() (*s3.Client, error) {
if s.s3Client == nil {
const defaultRegion = "us-east-1"
resolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: s.StorageConfig.S3.Endpoint, // or where ever you ran minio
SigningRegion: defaultRegion,
HostnameImmutable: true,
}, 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
cfg := aws.Config{
Credentials: creds,
Region: defaultRegion,
EndpointResolver: resolver,
}
s.s3Client = s3.NewFromConfig(cfg)
}
@ -43,7 +52,11 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
Prefix: aws.String(path.Join(s.StorageConfig.S3.Prefix, dirPath)),
}
result, err := s.s3Client.ListObjectsV2(context.TODO(), listObjectsInput)
client, err := s.getClient()
if err != nil {
return nil, err
}
result, err := client.ListObjectsV2(context.TODO(), listObjectsInput)
if err != nil {
return nil, err
}
@ -51,23 +64,24 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
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,
})
if !util.IsVideoFile(path.Base(key)) {
continue
}
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)