Skip to content

Commit 55ead51

Browse files
committed
Merge branch 'v0.10.0' of https://github.com/spaceuptech/space-cloud into ee-v0.10.0
2 parents 4178174 + b4bbadf commit 55ead51

File tree

7 files changed

+53
-91
lines changed

7 files changed

+53
-91
lines changed

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ type FileStore struct {
142142
Enabled bool `json:"enabled" yaml:"enabled"`
143143
StoreType string `json:"storeType" yaml:"storeType"`
144144
Conn string `json:"conn" yaml:"conn"`
145+
Endpoint string `json:"endpoint" yaml:"endpoint"`
145146
Rules []*FileRule `json:"rules" yaml:"rules"`
146147
}
147148

modules/filestore/amazons3/amaxonS3.go

-23
This file was deleted.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package amazons3
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/spaceuptech/space-cloud/utils"
7+
)
8+
9+
// AmazonS3 holds the S3 driver session
10+
type AmazonS3 struct {
11+
client *session.Session
12+
}
13+
14+
// Init initializes an amazon s3 driver
15+
func Init(region, endpoint string) (*AmazonS3, error) {
16+
awsConf := &aws.Config{Region: aws.String(region)}
17+
if len(endpoint) > 0 {
18+
awsConf.Endpoint = aws.String(endpoint)
19+
}
20+
session, err := session.NewSession(awsConf)
21+
return &AmazonS3{client: session}, err
22+
}
23+
24+
// GetStoreType returns the file store type
25+
func (a *AmazonS3) GetStoreType() utils.FileStoreType {
26+
return utils.AmazonS3
27+
}
28+
29+
// Close gracefully closed the local filestore module
30+
func (a *AmazonS3) Close() error {
31+
return nil
32+
}

modules/filestore/amazons3/create.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,40 @@ package amazons3
22

33
import (
44
"context"
5-
"fmt"
65
"io"
76
"strings"
87

98
"github.com/aws/aws-sdk-go/aws"
10-
"github.com/aws/aws-sdk-go/aws/session"
119
"github.com/aws/aws-sdk-go/service/s3"
1210
"github.com/aws/aws-sdk-go/service/s3/s3manager"
1311
"github.com/spaceuptech/space-cloud/model"
1412
)
1513

14+
// CreateFile creates a file in S3
1615
func (a *AmazonS3) CreateFile(ctx context.Context, project string, req *model.CreateFileRequest, file io.Reader) error {
17-
sess, err := session.NewSession(&aws.Config{
18-
Region: aws.String(a.region),
19-
},
20-
)
21-
if err != nil {
22-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
23-
return err
24-
}
25-
uploader := s3manager.NewUploader(sess)
26-
_, err = uploader.Upload(&s3manager.UploadInput{
16+
uploader := s3manager.NewUploader(a.client)
17+
_, err := uploader.Upload(&s3manager.UploadInput{
2718
Bucket: aws.String(project),
2819
Key: aws.String(req.Path + "/" + req.Name),
2920
Body: file,
3021
})
3122
return err
3223
}
3324

25+
// CreateDir creates a directory in S3
3426
func (a *AmazonS3) CreateDir(ctx context.Context, project string, req *model.CreateFileRequest) error {
35-
sess, err := session.NewSession(&aws.Config{
36-
Region: aws.String(a.region),
37-
},
38-
)
39-
if err != nil {
40-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
41-
return err
42-
}
43-
4427
path := req.Path
4528
// back slash at the end is important, if not then file will be created of that name
4629
if !strings.HasSuffix(path, "/") {
4730
path = req.Path + "/"
4831
}
4932

50-
svc := s3.New(sess)
33+
svc := s3.New(a.client)
5134
request := &s3.PutObjectInput{
5235
Bucket: aws.String(project),
5336
Key: aws.String(req.Path),
5437
}
55-
_, err = svc.PutObject(request)
38+
_, err := svc.PutObject(request)
5639
return err
5740
// return errors.New("Not Implemented")
5841
}

modules/filestore/amazons3/delete.go

+5-21
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@ package amazons3
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/aws/aws-sdk-go/aws"
8-
"github.com/aws/aws-sdk-go/aws/session"
97
"github.com/aws/aws-sdk-go/service/s3"
108
"github.com/aws/aws-sdk-go/service/s3/s3manager"
119
)
1210

11+
// DeleteFile deletes a file from S3
1312
func (a *AmazonS3) DeleteFile(ctx context.Context, project, path string) error {
14-
session, err := session.NewSession(&aws.Config{
15-
Region: aws.String(a.region),
16-
},
17-
)
18-
if err != nil {
19-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
20-
return err
21-
}
22-
svc := s3.New(session)
23-
_, err = svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(project), Key: aws.String(path)})
13+
svc := s3.New(a.client)
14+
_, err := svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(project), Key: aws.String(path)})
2415
if err != nil {
2516
return err
2617
}
@@ -31,17 +22,10 @@ func (a *AmazonS3) DeleteFile(ctx context.Context, project, path string) error {
3122
})
3223
}
3324

25+
// DeleteDir deletes a directory in S3
3426
func (a *AmazonS3) DeleteDir(ctx context.Context, project, path string) error {
35-
session, err := session.NewSession(&aws.Config{
36-
Region: aws.String(a.region),
37-
},
38-
)
39-
if err != nil {
40-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
41-
return err
42-
}
4327
// TODO: Consider AWS operation limit
44-
svc := s3.New(session)
28+
svc := s3.New(a.client)
4529

4630
// Setup BatchDeleteIterator to iterate through a list of objects.
4731
iter := s3manager.NewDeleteListIterator(svc, &s3.ListObjectsInput{

modules/filestore/amazons3/read.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@ package amazons3
33
import (
44
"bufio"
55
"context"
6-
"fmt"
76
"io/ioutil"
87
"os"
98
"path/filepath"
109

1110
"github.com/aws/aws-sdk-go/aws"
12-
"github.com/aws/aws-sdk-go/aws/session"
1311
"github.com/aws/aws-sdk-go/service/s3"
1412
"github.com/aws/aws-sdk-go/service/s3/s3manager"
1513
uuid "github.com/satori/go.uuid"
1614
"github.com/spaceuptech/space-cloud/model"
1715
)
1816

17+
// ListDir lists a directory in S3
1918
func (a *AmazonS3) ListDir(ctx context.Context, project string, req *model.ListFilesRequest) ([]*model.ListFilesResponse, error) {
20-
sess, err := session.NewSession(&aws.Config{
21-
Region: aws.String(a.region),
22-
},
23-
)
24-
if err != nil {
25-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
26-
return nil, err
27-
}
28-
svc := s3.New(sess)
19+
svc := s3.New(a.client)
2920

3021
resp, _ := svc.ListObjects(&s3.ListObjectsInput{
3122
Bucket: aws.String(project),
@@ -52,22 +43,16 @@ func (a *AmazonS3) ListDir(ctx context.Context, project string, req *model.ListF
5243
return result, nil
5344
}
5445

46+
// ReadFile reads a file from S3
5547
func (a *AmazonS3) ReadFile(ctx context.Context, project, path string) (*model.File, error) {
5648
u2 := uuid.NewV4()
5749

5850
tmpfile, err := ioutil.TempFile("", u2.String())
5951
if err != nil {
6052
return nil, err
6153
}
62-
sess, err := session.NewSession(&aws.Config{
63-
Region: aws.String(a.region),
64-
},
65-
)
66-
if err != nil {
67-
fmt.Println("AmazonS3 Couldn't Establish Connection ", err)
68-
return nil, err
69-
}
70-
downloader := s3manager.NewDownloader(sess)
54+
55+
downloader := s3manager.NewDownloader(a.client)
7156

7257
_, err = downloader.Download(tmpfile,
7358
&s3.GetObjectInput{

modules/filestore/store.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"github.com/spaceuptech/space-cloud/model"
1010
"github.com/spaceuptech/space-cloud/utils"
1111

12+
"github.com/spaceuptech/space-cloud/modules/auth"
1213
"github.com/spaceuptech/space-cloud/modules/filestore/amazons3"
1314
"github.com/spaceuptech/space-cloud/modules/filestore/local"
14-
"github.com/spaceuptech/space-cloud/modules/auth"
1515
)
1616

1717
// Module is responsible for managing the file storage module
@@ -70,7 +70,7 @@ func (m *Module) SetConfig(conf *config.FileStore) error {
7070
}
7171

7272
// Create a new crud blocks
73-
s, err := initBlock(utils.FileStoreType(conf.StoreType), conf.Conn)
73+
s, err := initBlock(utils.FileStoreType(conf.StoreType), conf.Conn, conf.Endpoint)
7474
if err != nil {
7575
return err
7676
}
@@ -86,12 +86,12 @@ func (m *Module) IsEnabled() bool {
8686
return m.enabled
8787
}
8888

89-
func initBlock(fileStoreType utils.FileStoreType, connection string) (FileStore, error) {
89+
func initBlock(fileStoreType utils.FileStoreType, connection, endpoint string) (FileStore, error) {
9090
switch fileStoreType {
9191
case utils.Local:
9292
return local.Init(connection)
9393
case utils.AmazonS3:
94-
return amazons3.Init(connection) // connection is the aws region code
94+
return amazons3.Init(connection, endpoint) // connection is the aws region code
9595
default:
9696
return nil, utils.ErrInvalidParams
9797
}

0 commit comments

Comments
 (0)