This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
/
s3.go
87 lines (73 loc) · 2.15 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package mariner
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
)
const (
// environment variables
awsCredsEnvVar = "AWSCREDS"
userIDEnvVar = "USER_ID"
sharedVolumeNameEnvVar = "ENGINE_WORKSPACE"
// setting a max so as to prevent the error of having too many files being open at once
// need to investigate how high we can set this bound without running into problems
// for now, conservatively setting the bound to 32
maxConcurrent = 32
// resides in the task's working dir in s3
// contains list of files that need to be downloaded from s3 in order for this task to run
inputFileListName = "_mariner_s3_input.json"
)
// S3FileManager ..
type S3FileManager struct {
AWSConfig *aws.Config
S3BucketName string
MaxConcurrent int
}
func loadAWSConfig() (*aws.Config, error) {
secret := []byte(os.Getenv(awsCredsEnvVar))
creds := &awsCredentials{}
err := json.Unmarshal(secret, creds)
if err != nil {
return nil, fmt.Errorf("error unmarshalling aws secret: %v", err)
}
credsConfig := credentials.NewStaticCredentials(creds.ID, creds.Secret, "")
awsConfig := &aws.Config{
Region: aws.String(Config.Storage.S3.Region),
Credentials: credsConfig,
}
return awsConfig, nil
}
type awsCredentials struct {
ID string `json:"id"`
Secret string `json:"secret"`
}
func (fm *S3FileManager) setup() (err error) {
fm.AWSConfig, err = loadAWSConfig()
if err != nil {
return err
}
fm.S3BucketName = Config.Storage.S3.Name
fm.MaxConcurrent = maxConcurrent
return nil
}
func (fm *S3FileManager) newS3Session() *session.Session {
return session.Must(session.NewSession(fm.AWSConfig))
}
/*
converts filepath to the corresponding s3 location
-> maps the local "task working directory"
-- to the S3 "task working directory"
filepaths look like:
"/engine-workspace/path/to/file"
s3 keys look like:
"/userID/path/to/file"
so, replace "engine-workspace" with "userID"
*/
func (fm *S3FileManager) s3Key(path string, userID string) string {
key := strings.Replace(path, engineWorkspaceVolumeName, userID, 1)
return key
}