/
oss.go
116 lines (97 loc) · 3.07 KB
/
oss.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
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oss
import (
"bytes"
"context"
"reflect"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/google/uuid"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
kitmd "github.com/dapr/kit/metadata"
)
// AliCloudOSS is a binding for an AliCloud OSS storage bucket.
type AliCloudOSS struct {
metadata *ossMetadata
client *oss.Client
logger logger.Logger
}
type ossMetadata struct {
Endpoint string `json:"endpoint" mapstructure:"endpoint"`
AccessKeyID string `json:"accessKeyID" mapstructure:"accessKeyID"`
AccessKey string `json:"accessKey" mapstructure:"accessKey"`
Bucket string `json:"bucket" mapstructure:"bucket"`
}
// NewAliCloudOSS returns a new instance.
func NewAliCloudOSS(logger logger.Logger) bindings.OutputBinding {
return &AliCloudOSS{logger: logger}
}
// Init does metadata parsing and connection creation.
func (s *AliCloudOSS) Init(_ context.Context, metadata bindings.Metadata) error {
m, err := s.parseMetadata(metadata)
if err != nil {
return err
}
client, err := s.getClient(m)
if err != nil {
return err
}
s.metadata = m
s.client = client
return nil
}
func (s *AliCloudOSS) Operations() []bindings.OperationKind {
return []bindings.OperationKind{bindings.CreateOperation}
}
func (s *AliCloudOSS) Invoke(_ context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
key := ""
if val, ok := req.Metadata["key"]; ok && val != "" {
key = val
} else {
key = uuid.New().String()
s.logger.Debugf("key not found. generating key %s", key)
}
bucket, err := s.client.Bucket(s.metadata.Bucket)
if err != nil {
return nil, err
}
// Upload a byte array.
err = bucket.PutObject(key, bytes.NewReader(req.Data))
if err != nil {
return nil, err
}
return nil, err
}
func (s *AliCloudOSS) parseMetadata(meta bindings.Metadata) (*ossMetadata, error) {
var m ossMetadata
err := kitmd.DecodeMetadata(meta.Properties, &m)
if err != nil {
return nil, err
}
return &m, nil
}
func (s *AliCloudOSS) getClient(metadata *ossMetadata) (*oss.Client, error) {
client, err := oss.New(metadata.Endpoint, metadata.AccessKeyID, metadata.AccessKey)
if err != nil {
return nil, err
}
return client, nil
}
// GetComponentMetadata returns the metadata of the component.
func (s *AliCloudOSS) GetComponentMetadata() (metadataInfo metadata.MetadataMap) {
metadataStruct := ossMetadata{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.BindingType)
return
}