@@ -2,57 +2,40 @@ package amazons3
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"io"
7
6
"strings"
8
7
9
8
"github.com/aws/aws-sdk-go/aws"
10
- "github.com/aws/aws-sdk-go/aws/session"
11
9
"github.com/aws/aws-sdk-go/service/s3"
12
10
"github.com/aws/aws-sdk-go/service/s3/s3manager"
13
11
"github.com/spaceuptech/space-cloud/model"
14
12
)
15
13
14
+ // CreateFile creates a file in S3
16
15
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 {
27
18
Bucket : aws .String (project ),
28
19
Key : aws .String (req .Path + "/" + req .Name ),
29
20
Body : file ,
30
21
})
31
22
return err
32
23
}
33
24
25
+ // CreateDir creates a directory in S3
34
26
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
-
44
27
path := req .Path
45
28
// back slash at the end is important, if not then file will be created of that name
46
29
if ! strings .HasSuffix (path , "/" ) {
47
30
path = req .Path + "/"
48
31
}
49
32
50
- svc := s3 .New (sess )
33
+ svc := s3 .New (a . client )
51
34
request := & s3.PutObjectInput {
52
35
Bucket : aws .String (project ),
53
36
Key : aws .String (req .Path ),
54
37
}
55
- _ , err = svc .PutObject (request )
38
+ _ , err : = svc .PutObject (request )
56
39
return err
57
40
// return errors.New("Not Implemented")
58
41
}
0 commit comments