* feat(backend): handlers for mobile messages * feat(backend): new service template * feat(backend): save mobile session start and send batches to kafka * feat(backend): added mobile support to sink, ender and storage * helm(videostorage): added helm chart for a new service videostorage * fix(backend): added pointer to streing for userBrowser (because it's null for mobile sessions) * feat(backend): added MsgIOSBatchMeta handler to message iterator's logic * feat(backend): added ios ts parser to ender * feat(backend): enabled producing batch of messages to queue * feat(backend): removed iosstart from mob files * feat(backend): added new ios message types * feat(backend): added iosStart and iosEnd * fix(backend): fixed log issue * feat(backend): send tar.gz archives to special queue topic * feat(backend): read raw archives from kafka * fix(backend): added missing file * fix(backend): removed the second file reading * fix(backend): fixed wrong queue topic name * fix(backend): fixed mobile trigger topic name * feat(backend): added tar.gz extractor and iOSSessionEnd handler * feat(backend): debug logs on message uploading * fix(backend): added raw-images topic consumption * feat(backend): now sink send iosSessionEnd to video-storage * feat(backend): added dir creator for new sessions * feat(backend): added correct command to execute * feat(backend): added overwrite option * feat(backend): added s3 uploader for video session replays * feat(backend): new consumer group for mobile sessions * feat(backend): debug logs for uploader * feat(backend): removed unused log * feat(backend): fixed s3 key for video replays * feat(backend): removed debug code * feat(backend): fixed video-storage message filter * fix(backend): added mobileSessionEnd to SessionEnd converter * feat(backend): added first version if db support for mobile events * fix(backend): added swipe events to mob file * feat(backend): added swipe event to pg * feat(backend): split logic into 2 services: image-storage and video-storage * feat(backend): added helm chart for image-storage service * fix(backend): fixed table name for mobile taps * feat(backend): added metadata handler for mobile message parser + fix message filters * feat(backend): added iosRawTopic to DB message consumer * fix(backend): removed value from mobile inputs * feat(backend): removed debug log from iterator * feat(backend): added new apple devices to iOS device parser * fix(backend): added real projectID instead of 0 * feat(backend): extended a list of simulators for device detector * feat(backend): updated networkCall mobile message * fix(backend): added new way to define is network call successed or not * feat(backend): added timezone support for mobile start request * feat(backend): added 2 mobile events Input and Click to mob file * feat(backend): refactored image storage * feat(backend): video storage with 2 workers * feat(backend): added project's platform support * feat(backend): added memory size field for mobile start request * feat(backend): changed video preset for ultrafast * feat(backend): added debug log to http /late handler * feat(backend): added debug log to db service for iosCrash messages * feat(backend): added tapRage event handler to heuristics * feat(backend): changed table and field names for ios crashes * feat(backend): added payload for tapRage events * feat(backend): added TapRage events insert to DB * feat(backend): added fps value to /mobile/start response * feat(backend): added image quality parameter to /mobile/start response * feat(backend): added ScreenLeave handler * feat(backend): removed screenEnter and screenLeave events, added new viewComponent event --------- Co-authored-by: rjshrjndrn <rjshrjndrn@gmail.com>
208 lines
4.6 KiB
Go
208 lines
4.6 KiB
Go
package s3
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
_session "github.com/aws/aws-sdk-go/aws/session"
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
|
|
objConfig "openreplay/backend/internal/config/objectstorage"
|
|
"openreplay/backend/pkg/objectstorage"
|
|
)
|
|
|
|
const MAX_RETURNING_COUNT = 40
|
|
|
|
type storageImpl struct {
|
|
uploader *s3manager.Uploader
|
|
svc *s3.S3
|
|
bucket *string
|
|
fileTag string
|
|
}
|
|
|
|
func NewS3(cfg *objConfig.ObjectsConfig) (objectstorage.ObjectStorage, error) {
|
|
if cfg == nil {
|
|
return nil, fmt.Errorf("s3 config is nil")
|
|
}
|
|
config := &aws.Config{
|
|
Region: aws.String(cfg.AWSRegion),
|
|
Credentials: credentials.NewStaticCredentials(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey, ""),
|
|
}
|
|
if cfg.AWSEndpoint != "" {
|
|
config.Endpoint = aws.String(cfg.AWSEndpoint)
|
|
config.DisableSSL = aws.Bool(true)
|
|
config.S3ForcePathStyle = aws.Bool(true)
|
|
|
|
if cfg.AWSSkipSSLValidation {
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
client := &http.Client{Transport: tr}
|
|
config.HTTPClient = client
|
|
}
|
|
}
|
|
sess, err := _session.NewSession(config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("AWS session error: %v", err)
|
|
}
|
|
return &storageImpl{
|
|
uploader: s3manager.NewUploader(sess),
|
|
svc: s3.New(sess), // AWS Docs: "These clients are safe to use concurrently."
|
|
bucket: &cfg.BucketName,
|
|
fileTag: loadFileTag(),
|
|
}, nil
|
|
}
|
|
|
|
func (s *storageImpl) tagging() *string {
|
|
return &s.fileTag
|
|
}
|
|
|
|
func (s *storageImpl) Upload(reader io.Reader, key string, contentType string, compression objectstorage.CompressionType) error {
|
|
cacheControl := "max-age=2628000, immutable, private"
|
|
var contentEncoding *string
|
|
switch compression {
|
|
case objectstorage.Gzip:
|
|
gzipStr := "gzip"
|
|
contentEncoding = &gzipStr
|
|
case objectstorage.Brotli:
|
|
gzipStr := "br"
|
|
contentEncoding = &gzipStr
|
|
}
|
|
|
|
_, err := s.uploader.Upload(&s3manager.UploadInput{
|
|
Body: reader,
|
|
Bucket: s.bucket,
|
|
Key: &key,
|
|
ContentType: &contentType,
|
|
CacheControl: &cacheControl,
|
|
ContentEncoding: contentEncoding,
|
|
Tagging: s.tagging(),
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (s *storageImpl) Get(key string) (io.ReadCloser, error) {
|
|
out, err := s.svc.GetObject(&s3.GetObjectInput{
|
|
Bucket: s.bucket,
|
|
Key: &key,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Body, nil
|
|
}
|
|
|
|
func (s *storageImpl) GetAll(key string) ([]io.ReadCloser, error) {
|
|
out, err := s.svc.GetObject(&s3.GetObjectInput{
|
|
Bucket: s.bucket,
|
|
Key: &key,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []io.ReadCloser{out.Body}, nil
|
|
}
|
|
|
|
func downloadS3Files(bucket, prefix string) {
|
|
sess := _session.Must(_session.NewSession(&aws.Config{
|
|
Region: aws.String("us-west-1"), // Change this to your region
|
|
}))
|
|
svc := s3.New(sess)
|
|
|
|
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: &bucket, Prefix: &prefix})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, item := range resp.Contents {
|
|
file, err := os.Create(*item.Key)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
downloader := s3manager.NewDownloader(sess)
|
|
_, err = downloader.Download(file,
|
|
&s3.GetObjectInput{
|
|
Bucket: &bucket,
|
|
Key: item.Key,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *storageImpl) Exists(key string) bool {
|
|
_, err := s.svc.HeadObject(&s3.HeadObjectInput{
|
|
Bucket: s.bucket,
|
|
Key: &key,
|
|
})
|
|
if err == nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *storageImpl) GetCreationTime(key string) *time.Time {
|
|
ans, err := s.svc.HeadObject(&s3.HeadObjectInput{
|
|
Bucket: s.bucket,
|
|
Key: &key,
|
|
})
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return ans.LastModified
|
|
}
|
|
|
|
func (s *storageImpl) GetFrequentlyUsedKeys(projectID uint64) ([]string, error) {
|
|
prefix := strconv.FormatUint(projectID, 10) + "/"
|
|
output, err := s.svc.ListObjectsV2(&s3.ListObjectsV2Input{
|
|
Bucket: s.bucket,
|
|
Prefix: &prefix,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
//pagination may be here
|
|
|
|
list := output.Contents
|
|
max := len(list)
|
|
if max > MAX_RETURNING_COUNT {
|
|
max = MAX_RETURNING_COUNT
|
|
sort.Slice(list, func(i, j int) bool {
|
|
return list[i].LastModified.After(*(list[j].LastModified))
|
|
})
|
|
}
|
|
|
|
var keyList []string
|
|
st := len(prefix)
|
|
for _, obj := range list[:max] {
|
|
keyList = append(keyList, (*obj.Key)[st:])
|
|
}
|
|
return keyList, nil
|
|
}
|
|
|
|
func loadFileTag() string {
|
|
// Load file tag from env
|
|
key := "retention"
|
|
value := os.Getenv("RETENTION")
|
|
if value == "" {
|
|
value = "default"
|
|
}
|
|
// Create URL encoded tag set for file
|
|
params := url.Values{}
|
|
params.Add(key, value)
|
|
return params.Encode()
|
|
}
|