-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
118 lines (104 loc) · 3.28 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package awss3
import (
"time"
"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"
"github.com/kozmoai/builder-backend/src/utils/config"
)
type AWSConfig struct {
Endpoint string
AccessKeyID string
AccessKeySecret string
Region string
BucketName string
UploadTimeout time.Duration
DriveType int
}
const (
DRIVE_TYPE_SYSTEM = 1
DRIVE_TYPE_TEAM = 2
)
const AWS_DRIVE_URL_KOZMO_CLOUD = "kozmo-cloud-storage.kozmoai.com.s3.ap-northeast-1.amazonaws.com"
const AWS_DRIVE_URL_KOZMO_CLOUD_TEAM = "kozmo-cloud-team-storage.kozmoai.com.s3.ap-northeast-1.amazonaws.com"
const KOZMO_DRIVE_URL_KOZMO_CLOUD = "kozmo-cloud-storage.kozmoai.com"
const KOZMO_DRIVE_URL_KOZMO_CLOUD_TEAM = "kozmo-cloud-team-storage.kozmoai.com"
func NewAWSConfig(endpoint string, accessKeyID string, accessKeySecret string, region string, bucketName string, uploadTimeout int) *AWSConfig {
timeout := time.Duration(uploadTimeout)
return &AWSConfig{
Endpoint: endpoint,
AccessKeyID: accessKeyID,
AccessKeySecret: accessKeySecret,
Region: region,
BucketName: bucketName,
UploadTimeout: timeout,
}
}
func NewSystemAwsConfigByGlobalConfig(config *config.Config) *AWSConfig {
return &AWSConfig{
Endpoint: config.GetAWSS3Endpoint(),
AccessKeyID: config.GetAWSS3AccessKeyID(),
AccessKeySecret: config.GetAWSS3AccessKeySecret(),
Region: config.GetAWSS3Region(),
BucketName: config.GetAWSS3SystemBucketName(),
UploadTimeout: config.GetAWSS3Timeout(),
DriveType: DRIVE_TYPE_SYSTEM,
}
}
func NewTeamAwsConfigByGlobalConfig(config *config.Config) *AWSConfig {
return &AWSConfig{
Endpoint: config.GetAWSS3Endpoint(),
AccessKeyID: config.GetAWSS3AccessKeyID(),
AccessKeySecret: config.GetAWSS3AccessKeySecret(),
Region: config.GetAWSS3Region(),
BucketName: config.GetAWSS3TeamBucketName(),
UploadTimeout: config.GetAWSS3Timeout(),
DriveType: DRIVE_TYPE_TEAM,
}
}
func CreateAWSSession(awsConfig *AWSConfig) *session.Session {
sess := session.Must(session.NewSession(
&aws.Config{
Endpoint: aws.String(awsConfig.Endpoint),
Region: aws.String(awsConfig.Region),
Credentials: credentials.NewStaticCredentials(
awsConfig.AccessKeyID,
awsConfig.AccessKeySecret,
"",
),
},
))
return sess
}
func CreateS3Session(awsSession *session.Session) *s3.S3 {
s3Session := s3.New(awsSession)
return s3Session
}
type S3Drive struct {
Instance *s3.S3
Session *session.Session
Config *AWSConfig
}
func NewS3Drive(awsConfig *AWSConfig) *S3Drive {
s3Drive := &S3Drive{
Config: awsConfig,
}
s3Drive.Session = CreateAWSSession(awsConfig)
s3Drive.Instance = CreateS3Session(s3Drive.Session)
return s3Drive
}
func (s3Drive *S3Drive) GetPreSignedPutURL(fileName string) (string, error) {
// get put request
req, _ := s3Drive.Instance.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(s3Drive.Config.BucketName),
Key: aws.String(fileName), // also you can define path in it.
ACL: aws.String("public-read"),
})
// sign timeout
url, errInPresign := req.Presign(s3Drive.Config.UploadTimeout)
if errInPresign != nil {
return "", errInPresign
}
return url, nil
}