owncast/core/storageproviders/s3Storage.go

120 lines
2.7 KiB
Go
Raw Normal View History

package storageproviders
2020-06-03 11:34:05 +03:00
import (
"bufio"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gabek/owncast/config"
"github.com/gabek/owncast/models"
2020-06-03 11:34:05 +03:00
)
//S3Storage is the s3 implementation of the ChunkStorageProvider
2020-06-03 11:34:05 +03:00
type S3Storage struct {
sess *session.Session
host string
s3Endpoint string
2020-06-03 11:34:05 +03:00
s3Region string
s3Bucket string
s3AccessKey string
s3Secret string
}
//Setup sets up the s3 storage for saving the video to s3
func (s *S3Storage) Setup() error {
log.Trace("Setting up S3 for external storage of video...")
2020-06-03 11:34:05 +03:00
s.s3Endpoint = config.Config.S3.Endpoint
s.s3Region = config.Config.S3.Region
s.s3Bucket = config.Config.S3.Bucket
s.s3AccessKey = config.Config.S3.AccessKey
s.s3Secret = config.Config.S3.Secret
2020-06-03 11:34:05 +03:00
s.sess = s.connectAWS()
return nil
2020-06-03 11:34:05 +03:00
}
//Save saves the file to the s3 bucket
func (s *S3Storage) Save(filePath string, retryCount int) (string, error) {
2020-06-03 11:34:05 +03:00
// fmt.Println("Saving", filePath)
2020-06-09 19:31:27 +03:00
file, err := os.Open(filePath)
2020-06-03 11:34:05 +03:00
if err != nil {
return "", err
2020-06-03 11:34:05 +03:00
}
defer file.Close()
2020-06-03 11:34:05 +03:00
uploader := s3manager.NewUploader(s.sess)
response, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s.s3Bucket), // Bucket to be used
Key: aws.String(filePath), // Name of the file to be saved
Body: file, // File
})
if err != nil {
log.Trace("error uploading:", err.Error())
if retryCount < 4 {
log.Trace("Retrying...")
return s.Save(filePath, retryCount+1)
}
2020-06-03 11:34:05 +03:00
}
// fmt.Println("Uploaded", filePath, "to", response.Location)
return response.Location, nil
2020-06-03 11:34:05 +03:00
}
//GenerateRemotePlaylist implements the 'GenerateRemotePlaylist' method
func (s *S3Storage) GenerateRemotePlaylist(playlist string, variant models.Variant) string {
2020-06-03 11:34:05 +03:00
var newPlaylist = ""
scanner := bufio.NewScanner(strings.NewReader(playlist))
for scanner.Scan() {
line := scanner.Text()
if line[0:1] != "#" {
fullRemotePath := variant.GetSegmentForFilename(line)
if fullRemotePath == nil {
2020-06-09 11:52:15 +03:00
line = ""
} else {
line = fullRemotePath.RemoteID
2020-06-09 11:52:15 +03:00
}
2020-06-03 11:34:05 +03:00
}
newPlaylist = newPlaylist + line + "\n"
}
return newPlaylist
}
func (s S3Storage) connectAWS() *session.Session {
creds := credentials.NewStaticCredentials(s.s3AccessKey, s.s3Secret, "")
_, err := creds.Get()
if err != nil {
2020-06-18 09:01:49 +03:00
log.Panicln(err)
2020-06-03 11:34:05 +03:00
}
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(s.s3Region),
Credentials: creds,
Endpoint: aws.String(s.s3Endpoint),
S3ForcePathStyle: aws.Bool(true),
2020-06-03 11:34:05 +03:00
},
)
if err != nil {
2020-06-18 09:01:49 +03:00
log.Panicln(err)
2020-06-03 11:34:05 +03:00
}
return sess
}