forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
111 lines (87 loc) · 3.34 KB
/
repository.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 minio
import (
"bytes"
"context"
"io"
"time"
"github.com/kyma-project/kyma/components/application-connector/internal/apperrors"
"github.com/minio/minio-go"
)
const (
timeoutDuration = time.Duration(5)
secureTraffic = false
bucketLocation = "us-east-1"
)
type Client interface {
PutObjectWithContext(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (n int64, err error)
GetObjectWithContext(ctx context.Context, bucketName, objectName string, opts minio.GetObjectOptions) (*minio.Object, error)
RemoveObject(bucketName, objectName string) error
BucketExists(bucketName string) (found bool, err error)
MakeBucket(bucketName, location string) error
}
type Repository interface {
Put(bucketName string, objectName string, resource []byte) apperrors.AppError
Get(bucketName string, objectName string) ([]byte, apperrors.AppError)
Remove(bucketName string, objectName string) apperrors.AppError
}
type repository struct {
minioClient Client
timeout time.Duration
bucketLocation string
}
func NewMinioRepository(endpoint string, accessKeyID string, secretAccessKey string) (Repository, apperrors.AppError) {
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, secureTraffic)
if err != nil {
return nil, apperrors.Internal("error while creating Minio client: %s", err)
}
return &repository{minioClient: minioClient, timeout: timeoutDuration, bucketLocation: bucketLocation}, nil
}
func (r *repository) Put(bucketName string, objectName string, resource []byte) apperrors.AppError {
contextWithTimeout, cancel := context.WithTimeout(context.Background(), r.timeout*time.Second)
defer cancel()
reader := bytes.NewReader(resource)
_, appErr := r.minioClient.PutObjectWithContext(contextWithTimeout, bucketName, objectName, reader, reader.Size(), minio.PutObjectOptions{})
if appErr != nil {
return apperrors.Internal("error while uploading file to Minio: %s", appErr)
}
return nil
}
func (r *repository) Get(bucketName string, objectName string) ([]byte, apperrors.AppError) {
object, err, cancel := r.getObject(bucketName, objectName)
defer cancel()
if err != nil {
return nil, err
}
data, err := readBytes(object)
if err != nil {
return nil, err
}
return data, nil
}
func (r *repository) Remove(bucketName string, objectName string) apperrors.AppError {
err := r.minioClient.RemoveObject(bucketName, objectName)
if err != nil {
return apperrors.Internal("failed to remove object %s", objectName, err)
}
return nil
}
func (r *repository) getObject(bucketName string, objectName string) (*minio.Object, apperrors.AppError, func()) {
contextWithTimeout, cancel := context.WithTimeout(context.Background(), r.timeout*time.Second)
object, err := r.minioClient.GetObjectWithContext(contextWithTimeout, bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, apperrors.Internal("failed to get object %s: ", objectName, err), cancel
}
return object, nil, cancel
}
func readBytes(reader io.Reader) ([]byte, apperrors.AppError) {
buffer := bytes.Buffer{}
_, err := buffer.ReadFrom(reader)
if err != nil {
errorResponse := err.(minio.ErrorResponse)
if errorResponse.Code == "NoSuchKey" {
return nil, nil
}
return nil, apperrors.Internal("error while reading object bytes: %s", err)
}
return buffer.Bytes(), nil
}