-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
111 lines (99 loc) · 2.42 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
package aws_s3
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"io/ioutil"
"log"
"os"
)
type Service struct {
Client *s3.S3
EndPoint string
Region string
}
const DefaultClientName = "default-s3-client"
var clients map[string]*Service
var StdLogger stdLogger
type stdLogger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}
func init() {
StdLogger = log.New(os.Stdout, "[breaker] ", log.LstdFlags|log.Lshortfile)
}
func InitService(clientID, sk, token, region, point string) error {
credential := credentials.NewStaticCredentials(clientID, sk, token)
cfg := aws.NewConfig().WithCredentials(credential).WithRegion(region).
WithEndpoint(point).WithS3ForcePathStyle(true).WithDisableSSL(true)
sess, err := session.NewSession(cfg)
if err != nil {
return err
}
if len(clients) == 0 {
clients = make(map[string]*Service, 0)
}
clients[clientID] = &Service{
Client: s3.New(sess),
EndPoint: point,
Region: region,
}
return nil
}
func GetS3Client(clientName string) *Service {
if client, ok := clients[clientName]; ok {
return client
}
panic(fmt.Sprintf("client %s has not initial", clientName))
}
func (s *Service) GetObj(key, bucket string) ([]byte, error) {
inputObject := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
out, err := s.Client.GetObject(inputObject)
if err != nil && !IsNotFoundErr(err) {
return nil, err
}
if out.Body != nil {
res, err := ioutil.ReadAll(out.Body)
if err != nil {
return nil, err
}
return res, nil
}
err = awserr.New(s3.ErrCodeNoSuchKey, "empty body", nil)
return nil, err
}
func (s *Service) PutObj(key, bucket string, data []byte) error {
inputObject := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ContentType: aws.String("application/octet-stream"),
Body: bytes.NewReader(data),
}
out, err := s.Client.PutObject(inputObject)
if err != nil {
outStr := ""
if out != nil {
outStr = out.String()
}
StdLogger.Print("PutS3Object ", err, outStr)
return err
}
return nil
}
func IsNotFoundErr(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case s3.ErrCodeNoSuchKey:
return true
}
}
return false
}