user1949387
user1949387

Reputation: 1285

Golang how can I integrate new package into my image upload code

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 ?

Upvotes: 0

Views: 560

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109475

You're saving the new image to a file, then not doing anything with that file. You could read the file off the disk if you want, or you could encode it in memory:

...
resized := imaging.Resize(fileOpen, 300, 300, imaging.Lanczos)

var buf byte.Buffer
err = imaging.Encode(&buf, resized, imaging.JPEG)
if err != nil {
    log.Println(err)
    return
}

params := &s3.PutObjectInput{
    Bucket: aws.String("amazon-bucket"),
    Key:    aws.String(path),
    Body:   buf.Bytes(),
}
...

Upvotes: 2

Related Questions