Reputation: 72
How to upload an image file in Google Cloud Storage that I already resized using 3rd party package "github.com/disintegration/imaging"
?
I tried this
Code:
f, uploadedFile, err := c.Request.FormFile("file") // image file
// Decode the file into a image struct
srcImg, _, err:= image.Decode(f)
// Resize srcImage to width = 800px preserving the aspect ratio.
adjustedFile := imaging.Resize(srcImg, 800, 0, imaging.Lanczos)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
"error": true,
})
return
}
defer f.Close()
sw := storageClient.Bucket(bucket).Object(""+ callType +"/"+ yearString +"/"+ monthString +"/" + uploadedFile.Filename).NewWriter(ctx)
if _, err := io.Copy(sw, adjustedFile); err != nil { // error here
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
"error": true,
})
return
}
Error:
`cannot use adjustedFile (type *image.NRGBA) as type io.Reader in a`rgument to io.Copy:
*image.NRGBA does not implement io.Reader (missing Read method)
Upvotes: 0
Views: 399
Reputation: 5829
Posting this as a community wiki, since It's based on @CeriseLimon's comment.
The problem is that you have to encode your image to bytes before sending it to GCS and you can do it by using the image/jpeg package, try the following code
if _, err := jpeg.Encode(sw, adjustedFile, &jpeg.Options{Quality:77}); err != nil {
...
}
Upvotes: 1