-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
90 lines (80 loc) · 2.68 KB
/
s3.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package s3
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/gazebo-web/cloudsim/pkg/storage"
"github.com/gazebo-web/gz-go/v7"
"net/http"
"path/filepath"
"time"
)
// NewAPI returns an S3 client from the given config provider.
func NewAPI(config client.ConfigProvider) s3iface.S3API {
return s3.New(config)
}
// s3Storage is a storage.Storage implementation.
type s3Storage struct {
API s3iface.S3API
Logger gz.Logger
}
func (s s3Storage) PrepareAddress(bucket, key string) string {
return fmt.Sprintf("s3://%s", filepath.Join(bucket, key))
}
// Upload uploads a file to the cloud storage.
func (s s3Storage) Upload(input storage.UploadInput) error {
s.Logger.Debug(fmt.Sprintf("Upload input: %+v", input))
if input.File == nil {
return storage.ErrMissingFile
}
var bslice []byte
_, err := input.File.Read(bslice)
if err != nil {
s.Logger.Debug(fmt.Sprintf("Reading file failed. Error: %s", err))
return err
}
if http.DetectContentType(bslice) != input.ContentType {
s.Logger.Debug(fmt.Sprintf("Invalid content type. Actual: %s. Expected: %s.", http.DetectContentType(bslice), input.ContentType))
return storage.ErrBadContentType
}
_, err = s.API.PutObject(&s3.PutObjectInput{
Bucket: &input.Bucket,
Key: &input.Key,
ACL: aws.String("private"),
Body: input.File,
ContentLength: aws.Int64(input.ContentLength),
ContentType: aws.String(input.ContentType),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: aws.String("AES256"),
})
if err != nil {
s.Logger.Debug(fmt.Sprintf("Uploading object failed. Error: %s", err))
return err
}
s.Logger.Debug(fmt.Sprintf("Uploading file to bucket [%s] succeeded.", input.Bucket))
return nil
}
// GetURL returns an URL to access the given bucket with the given key.
func (s s3Storage) GetURL(bucket string, key string, expiresIn time.Duration) (string, error) {
s.Logger.Debug(fmt.Sprintf("Getting URL for bucket [%s] and key [%s]. Expiration in: %s", bucket, key, expiresIn.String()))
req, _ := s.API.GetObjectRequest(&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
u, err := req.Presign(expiresIn)
if err != nil {
s.Logger.Debug(fmt.Sprintf("Presigning URL failed. Error: %s", err))
return "", err
}
s.Logger.Debug(fmt.Sprintf("Getting URL succeeded. URL: %s", u))
return u, nil
}
// NewStorage initializes a new storage.Storage implementation using s3.
func NewStorage(api s3iface.S3API, logger gz.Logger) storage.Storage {
return &s3Storage{
API: api,
Logger: logger,
}
}