69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"SimpleTutorialHosting/internal/models"
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||
|
"github.com/aws/aws-sdk-go-v2/config"
|
||
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||
|
)
|
||
|
|
||
|
type S3Client struct {
|
||
|
client *s3.Client
|
||
|
bucketName string
|
||
|
}
|
||
|
|
||
|
func NewS3Client(ctx context.Context, bucketName string) (*S3Client, error) {
|
||
|
cfg, err := config.LoadDefaultConfig(ctx)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
client := s3.NewFromConfig(cfg)
|
||
|
return &S3Client{
|
||
|
client: client,
|
||
|
bucketName: bucketName,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (s *S3Client) Upload(ctx context.Context, key string, body []byte) {
|
||
|
//todo
|
||
|
}
|
||
|
|
||
|
func (s *S3Client) GetConfig(ctx context.Context) (models.Config, error) {
|
||
|
var config models.Config
|
||
|
|
||
|
result, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
||
|
Bucket: &s.bucketName,
|
||
|
Key: aws.String("config.json"), //todo
|
||
|
})
|
||
|
if err != nil {
|
||
|
return config, err
|
||
|
}
|
||
|
defer result.Body.Close()
|
||
|
|
||
|
if err := json.NewDecoder(result.Body).Decode(&config); err != nil {
|
||
|
return config, err
|
||
|
}
|
||
|
|
||
|
return config, nil
|
||
|
}
|
||
|
|
||
|
func (s *S3Client) SaveConfig(ctx context.Context, config models.Config) {
|
||
|
//todo
|
||
|
}
|
||
|
|
||
|
func (s *S3Client) StreamVideo(ctx context.Context, key string, rangeHeader string) (*s3.GetObjectOutput, error) {
|
||
|
input := &s3.GetObjectInput{
|
||
|
Bucket: &s.bucketName,
|
||
|
Key: &key,
|
||
|
}
|
||
|
|
||
|
if rangeHeader != "" {
|
||
|
input.Range = &rangeHeader
|
||
|
}
|
||
|
|
||
|
return s.client.GetObject(ctx, input)
|
||
|
}
|