-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3.go
65 lines (58 loc) · 1.69 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
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/globalsign/mgo/bson"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
func UploadFile(bucket, region string, plot io.WriterTo) (string, error) {
s := session.Must(session.NewSession(&aws.Config{Region: aws.String(region)}))
_, err := s.Config.Credentials.Get()
f, err := ioutil.TempFile("", "promplot-*.png")
if err != nil {
return "", fmt.Errorf("failed to create tmp file: %v", err)
}
defer func() {
err = f.Close()
if err != nil {
panic(fmt.Errorf("failed to close tmp file: %v", err))
}
err := os.Remove(f.Name())
if err != nil {
panic(fmt.Errorf("failed to delete tmp file: %v", err))
}
}()
_, err = plot.WriteTo(f)
if err != nil {
return "", fmt.Errorf("failed to write plot to file: %v", err)
}
// get the file size and read
// the file content into a buffer
fileInfo, _ := f.Stat()
size := fileInfo.Size()
buffer := make([]byte, size)
_, err = f.Seek(0, io.SeekStart)
_, err = f.Read(buffer)
// create a unique file name for the file
tempFileName := "pictures/" + bson.NewObjectId().Hex() + "_" + strconv.FormatInt(time.Now().Unix(), 10) + ".png"
_, err = s3.New(s).PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(tempFileName),
ACL: aws.String("public-read"),
Body: bytes.NewReader(buffer),
ContentLength: aws.Int64(int64(size)),
ContentType: aws.String(http.DetectContentType(buffer)),
})
if err != nil {
return "", err
}
return fmt.Sprintf("https://%s.s3-%s.amazonaws.com/%s", bucket, region, tempFileName), err
}