Skip to content

Commit 1592556

Browse files
committed
feat: Add cli storage plugin.
1 parent 57b0778 commit 1592556

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

pkg/run/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (l *localServices) Start(pool worker.WorkerPool) error {
112112
os.Setenv(minio.MINIO_ACCESS_KEY_ENV, "minioadmin")
113113
os.Setenv(minio.MINIO_SECRET_KEY_ENV, "minioadmin")
114114

115-
sp, err := minio.New()
115+
sp, err := NewStorage()
116116
if err != nil {
117117
return err
118118
}

pkg/run/storage.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright Nitric Pty Ltd.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package run
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/aws/aws-sdk-go/aws"
22+
"github.com/aws/aws-sdk-go/aws/awserr"
23+
"github.com/aws/aws-sdk-go/aws/credentials"
24+
"github.com/aws/aws-sdk-go/aws/session"
25+
"github.com/aws/aws-sdk-go/service/s3"
26+
"github.com/nitrictech/nitric/pkg/plugins/storage"
27+
s3_service "github.com/nitrictech/nitric/pkg/plugins/storage/s3"
28+
"github.com/nitrictech/nitric/pkg/utils"
29+
)
30+
31+
type RunStorageService struct {
32+
storage.StorageService
33+
client *s3.S3
34+
}
35+
36+
const (
37+
MINIO_ENDPOINT_ENV = "MINIO_ENDPOINT"
38+
MINIO_ACCESS_KEY_ENV = "MINIO_ACCESS_KEY"
39+
MINIO_SECRET_KEY_ENV = "MINIO_SECRET_KEY"
40+
)
41+
42+
type minioConfig struct {
43+
endpoint string
44+
accessKey string
45+
secretKey string
46+
}
47+
48+
func (r *RunStorageService) ensureBucketExists(bucket string) error {
49+
_, err := r.client.HeadBucket(&s3.HeadBucketInput{
50+
Bucket: aws.String(bucket),
51+
})
52+
53+
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == s3.ErrCodeNoSuchBucket {
54+
_, err = r.client.CreateBucket(&s3.CreateBucketInput{
55+
Bucket: aws.String(bucket),
56+
GrantFullControl: aws.String("*"),
57+
})
58+
}
59+
60+
if err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
67+
func (r *RunStorageService) Read(bucket string, key string) ([]byte, error) {
68+
err := r.ensureBucketExists(bucket)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
return r.StorageService.Read(bucket, key)
74+
}
75+
76+
func (r *RunStorageService) Write(bucket string, key string, object []byte) error {
77+
err := r.ensureBucketExists(bucket)
78+
if err != nil {
79+
return err
80+
}
81+
82+
return r.StorageService.Write(bucket, key, object)
83+
}
84+
85+
func (r *RunStorageService) Delete(bucket string, key string) error {
86+
err := r.ensureBucketExists(bucket)
87+
if err != nil {
88+
return err
89+
}
90+
91+
return r.StorageService.Delete(bucket, key)
92+
}
93+
94+
func (r *RunStorageService) ListFiles(bucket string) ([]*storage.FileInfo, error) {
95+
err := r.ensureBucketExists(bucket)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
return r.StorageService.ListFiles(bucket)
101+
}
102+
103+
func (r *RunStorageService) PreSignUrl(bucket string, key string, operation storage.Operation, expiry uint32) (string, error) {
104+
err := r.ensureBucketExists(bucket)
105+
if err != nil {
106+
return "", err
107+
}
108+
109+
return r.StorageService.PreSignUrl(bucket, key, operation, expiry)
110+
}
111+
112+
func configFromEnv() (*minioConfig, error) {
113+
endpoint := utils.GetEnv(MINIO_ENDPOINT_ENV, "")
114+
accKey := utils.GetEnv(MINIO_ACCESS_KEY_ENV, "")
115+
secKey := utils.GetEnv(MINIO_SECRET_KEY_ENV, "")
116+
117+
configErrors := make([]error, 0)
118+
119+
if endpoint == "" {
120+
configErrors = append(configErrors, fmt.Errorf("%s not configured", MINIO_ENDPOINT_ENV))
121+
}
122+
123+
if accKey == "" {
124+
configErrors = append(configErrors, fmt.Errorf("%s not configured", MINIO_ACCESS_KEY_ENV))
125+
}
126+
127+
if secKey == "" {
128+
configErrors = append(configErrors, fmt.Errorf("%s not configured", MINIO_SECRET_KEY_ENV))
129+
}
130+
131+
if len(configErrors) > 0 {
132+
return nil, fmt.Errorf("configuration errors: %v", configErrors)
133+
}
134+
135+
return &minioConfig{
136+
endpoint: endpoint,
137+
accessKey: accKey,
138+
secretKey: secKey,
139+
}, nil
140+
}
141+
142+
func nameSelector(nitricName string) (*string, error) {
143+
return &nitricName, nil
144+
}
145+
146+
func NewStorage() (storage.StorageService, error) {
147+
conf, err := configFromEnv()
148+
if err != nil {
149+
return nil, err
150+
}
151+
152+
// Configure to use MinIO Server
153+
s3Config := &aws.Config{
154+
Credentials: credentials.NewStaticCredentials(conf.accessKey, conf.secretKey, ""),
155+
Endpoint: aws.String(conf.endpoint),
156+
Region: aws.String("us-east-1"),
157+
DisableSSL: aws.Bool(true),
158+
S3ForcePathStyle: aws.Bool(true),
159+
}
160+
newSession, err := session.NewSession(s3Config)
161+
if err != nil {
162+
return nil, fmt.Errorf("error creating new session")
163+
}
164+
165+
s3Client := s3.New(newSession)
166+
167+
s3Service, err := s3_service.NewWithClient(nil, s3Client, s3_service.WithSelector(nameSelector))
168+
169+
return &RunStorageService{
170+
StorageService: s3Service,
171+
client: s3Client,
172+
}, nil
173+
}

0 commit comments

Comments
 (0)