Specify image size when uploading to Minio
Not specifying it caused high memory usage.
Change-Id: I39ba4a7a0c35cc731228d31c03f237c5da99cd0c
diff --git a/mqtt_processor/mqtt_processor.go b/mqtt_processor/mqtt_processor.go
index e965702..35c067f 100644
--- a/mqtt_processor/mqtt_processor.go
+++ b/mqtt_processor/mqtt_processor.go
@@ -2,9 +2,12 @@
import (
"bufio"
+ "bytes"
"context"
"encoding/json"
"fmt"
+ "io"
+ "io/ioutil"
"net/url"
"strings"
"time"
@@ -53,13 +56,27 @@
return email.TextBody == "Y-Token: "+config.EmailToken
}
+func getSize(stream io.Reader) (io.Reader, int, error) {
+ b, err := ioutil.ReadAll(stream)
+ if err != nil {
+ return nil, -1, err
+ }
+
+ r := bytes.NewReader(b)
+ return r, r.Len(), nil
+}
+
func uploadImage(email *parsemail.Email, cameraID string, config *MQTTConfig, s3c *minio.Client) (string, error) {
for _, a := range email.Attachments {
if strings.HasPrefix(a.ContentType, "image") {
// Upload image
ctx := context.Background()
fileName := cameraID + "/" + a.Filename
- n, err := s3c.PutObject(ctx, config.S3Bucket, fileName, a.Data, -1, minio.PutObjectOptions{ContentType: a.ContentType})
+ data, size, err := getSize(a.Data)
+ if err != nil {
+ return "", err
+ }
+ n, err := s3c.PutObject(ctx, config.S3Bucket, fileName, data, int64(size), minio.PutObjectOptions{ContentType: a.ContentType})
if err != nil {
return "", err
}