forked from codahale/sneaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
41 lines (35 loc) · 863 Bytes
/
upload.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
package sneaker
import (
"bytes"
"io"
"io/ioutil"
fpath "path"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
// Upload encrypts the given secret with a KMS data key and uploads it to S3.
func (m *Manager) Upload(path string, r io.Reader) error {
plaintext, err := ioutil.ReadAll(r)
if err != nil {
return err
}
ciphertext, err := m.Envelope.Seal(m.KeyId, m.context(path), plaintext)
if err != nil {
return err
}
if _, err := m.Objects.PutObject(
&s3.PutObjectInput{
ContentLength: aws.Int64(int64(len(ciphertext))),
ContentType: aws.String(contentType),
Bucket: aws.String(m.Bucket),
Key: aws.String(fpath.Join(m.Prefix, path)),
Body: bytes.NewReader(ciphertext),
},
); err != nil {
return err
}
return nil
}
const (
contentType = "application/octet-stream"
)