I am new to Golang and have a function that uploads an image to my Amazon s3 account and saves it . Everything works great however I was looking for a way to resize images as some images are up to 4MB in size. I found a package that works correctly for resizing images https://github.com/disintegration/imaging . My question is how can I use the new resized image instead of the old file to upload ? For instance this is my original code that uploads an image and does not resize
func UploadProfile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var buff bytes.Buffer
var result string
sess, _ := "access keys"
svc := s3.New(sess)
file, handler, err := r.FormFile("file")
if err != nil {
fmt.Println("Error Uploading Image")
return
}
defer file.Close()
read_file,err := ioutil.ReadAll(file)
fileBytes := bytes.NewReader(read_file)
if err != nil {
fmt.Println("Error Reading file")
return
}
file.Read(read_file)
path := handler.Filename
params := &s3.PutObjectInput{
Bucket: aws.String("amazon-bucket"),
Key: aws.String(path),
Body: fileBytes,
}
resp, err := svc.PutObject(params)
if err != nil {
fmt.Printf("bad response: %s", err)
}
}
The issue I am now having is with this snippet below
file_open,err := imaging.Open("myImage.jpg")
if err != nil {
print("Imaging Open error")
}
new_image := imaging.Resize(file_open, 300, 300, imaging.Lanczos)
errs := imaging.Save(new_image, "dst.jpg")
if errs != nil {
print(err)
}
The code above gets any image and resizes it to 300 by 300 which is exactly what I want, now how can I use that new Image and send it to my s3 image bucket ? The part that confuses me is that the new_image is of type image.NRGBA while the original image is of type Multipart.File . When I upload an image to Amazon it wants to know how many bytes it has and I can't do something like
read_file,err := ioutil.ReadAll(new_image)
if err != nil {
fmt.Println("Error Reading file")
return
}
because it only expects something of type io.Reader is there any work around this ? so that I can save my new Image to Amazon s3 ?