5 Commits

Author SHA1 Message Date
83694864ad update to latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-04 12:20:46 +03:00
0e452f4dc7 rearrange init code
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-04 10:36:59 +03:00
560cb01564 extend tests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-04 09:44:13 +03:00
6efc858dbd fill region option in minio.Options
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-04 09:05:03 +03:00
392db01d40 update for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-01-29 14:45:37 +03:00
4 changed files with 65 additions and 39 deletions

3
go.mod
View File

@@ -4,6 +4,7 @@ go 1.15
require (
github.com/gabriel-vasile/mimetype v1.1.2
github.com/google/uuid v1.1.5
github.com/minio/minio-go/v7 v7.0.7
github.com/unistack-org/micro/v3 v3.1.8
github.com/unistack-org/micro/v3 v3.2.6
)

4
go.sum
View File

@@ -90,8 +90,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/unistack-org/micro/v3 v3.1.8 h1:ma9tBX7h0QyByDZ+vrY3+kqJoy/Ro9vMx2iTLjrDaNI=
github.com/unistack-org/micro/v3 v3.1.8/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
github.com/unistack-org/micro/v3 v3.2.6 h1:LfgoC86oF5tgsB5JVe8L0yF4swG4FGH0fVBP+FrG6fw=
github.com/unistack-org/micro/v3 v3.2.6/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

63
s3.go
View File

@@ -69,6 +69,7 @@ func (s *s3Store) Init(opts ...store.Option) error {
}
var akey, skey string
region := "us-east-1"
endpoint := s.endpoint
if s.opts.Context != nil {
@@ -80,38 +81,39 @@ func (s *s3Store) Init(opts ...store.Option) error {
}
if v, ok := s.opts.Context.Value(endpointKey{}).(string); ok && v != "" {
endpoint = v
var secure bool
if strings.HasPrefix(endpoint, "https://") || s.opts.TLSConfig != nil {
secure = true
}
if u, err := url.Parse(endpoint); err == nil && u.Host != "" {
endpoint = u.Host
}
ts, err := minio.DefaultTransport(secure)
if err != nil {
return fmt.Errorf("init error: %w", err)
}
if s.opts.TLSConfig != nil {
ts.TLSClientConfig = s.opts.TLSConfig
}
s.mopts.Transport = ts
s.mopts.Secure = secure
s.endpoint = endpoint
}
if v, ok := s.opts.Context.Value(regionKey{}).(string); ok && v != "" {
region = v
}
}
if len(akey) > 0 && len(skey) > 0 {
s.mopts.Creds = creds.NewStaticV2(akey, skey, "")
}
if len(endpoint) == 0 {
return fmt.Errorf("missing Endpoint option")
}
var secure bool
if strings.HasPrefix(endpoint, "https://") || s.opts.TLSConfig != nil {
secure = true
}
if u, err := url.Parse(endpoint); err == nil && u.Host != "" {
endpoint = u.Host
}
ts, err := minio.DefaultTransport(secure)
if err != nil {
return fmt.Errorf("init error: %w", err)
}
if s.opts.TLSConfig != nil {
ts.TLSClientConfig = s.opts.TLSConfig
}
s.mopts.Transport = ts
s.endpoint = endpoint
s.mopts.Region = region
return nil
}
@@ -186,13 +188,8 @@ func (s *s3Store) Write(ctx context.Context, key string, val interface{}, opts .
bucket = options.Namespace
}
var region string
mputopts := minio.PutObjectOptions{}
if v, ok := s.opts.Context.Value(regionKey{}).(string); ok && v != "" {
region = v
}
if options.Context != nil {
if v, ok := options.Context.Value(contentTypeKey{}).(string); ok && v != "" {
mputopts.ContentType = v
@@ -211,7 +208,7 @@ func (s *s3Store) Write(ctx context.Context, key string, val interface{}, opts .
if ok, err := s.client.BucketExists(ctx, bucket); err != nil {
return err
} else if !ok {
opts := minio.MakeBucketOptions{Region: region}
opts := minio.MakeBucketOptions{Region: s.mopts.Region}
if err := s.client.MakeBucket(ctx, bucket, opts); err != nil {
return err
}
@@ -302,3 +299,7 @@ func (s *s3Store) List(ctx context.Context, opts ...store.ListOption) ([]string,
func (s *s3Store) String() string {
return "s3"
}
func (s *s3Store) Name() string {
return s.opts.Name
}

View File

@@ -6,6 +6,8 @@ import (
"os"
"testing"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/unistack-org/micro/v3"
)
@@ -44,7 +46,7 @@ func TestStore(t *testing.T) {
}
}()
svc := micro.NewService(micro.Store(s))
svc := micro.NewService(micro.Stores(s))
if err := svc.Init(); err != nil {
t.Fatalf("service init failed: %v", err)
}
@@ -52,22 +54,23 @@ func TestStore(t *testing.T) {
val := []byte("test")
key := "key"
if err := s.Write(ctx, key, val, WriteBucket("micro-store-s3"), ContentType("text/plain")); err != nil {
bucket := "micro-store-s3-" + uuid.New().String()
if err := s.Write(ctx, key, val, WriteBucket(bucket), ContentType("text/plain")); err != nil {
t.Fatal(err)
}
val = nil
if err := s.Exists(ctx, key, ExistsBucket("micro-store-s3")); err != nil {
if err := s.Exists(ctx, key, ExistsBucket(bucket)); err != nil {
t.Fatal(err)
}
if err := s.Read(ctx, key, &val, ReadBucket("micro-store-s3")); err != nil {
if err := s.Read(ctx, key, &val, ReadBucket(bucket)); err != nil {
t.Fatal(err)
} else if !bytes.Equal(val, []byte("test")) {
t.Fatalf("read bytes are not equal %s != %s", val, "test")
}
names, err := s.List(ctx, ListBucket("micro-store-s3"))
names, err := s.List(ctx, ListBucket(bucket))
if err != nil {
t.Fatal(err)
}
@@ -83,4 +86,25 @@ func TestStore(t *testing.T) {
t.Fatalf("key not found in %v", names)
}
objectsCh := make(chan minio.ObjectInfo)
minioClient := s.(*s3Store).client
// Send object names that are needed to be removed to objectsCh
go func() {
defer close(objectsCh)
// List all objects from a bucket-name with a matching prefix.
for object := range minioClient.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{Recursive: true}) {
if object.Err != nil {
t.Fatal(object.Err)
}
objectsCh <- object
}
}()
opts := minio.RemoveObjectsOptions{
GovernanceBypass: true,
}
for rErr := range minioClient.RemoveObjects(context.Background(), bucket, objectsCh, opts) {
t.Fatalf("Error detected during deletion: %v", rErr)
}
}