/
create.go
36 lines (31 loc) · 1.05 KB
/
create.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
package amazons3
import (
"context"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/spaceuptech/space-cloud/gateway/model"
"github.com/spaceuptech/space-cloud/gateway/utils"
)
// CreateFile creates a file in S3
func (a *AmazonS3) CreateFile(ctx context.Context, req *model.CreateFileRequest, file io.Reader) error {
uploader := s3manager.NewUploader(a.client)
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(a.bucket),
Key: aws.String(utils.JoinLeading(req.Path, req.Name, "/")),
Body: file,
})
return err
}
// CreateDir creates a directory in S3
func (a *AmazonS3) CreateDir(ctx context.Context, req *model.CreateFileRequest) error {
// back slash at the end is important, if not then file will be created of that name
svc := s3.New(a.client)
request := &s3.PutObjectInput{
Bucket: aws.String(a.bucket),
Key: aws.String(utils.JoinLeadingTrailing(req.Path, req.Name, "/")),
}
_, err := svc.PutObject(request)
return err
}