当源文件是multipart.File时,image.Decode会返回”unknown format”错误。

huangapple go评论89阅读模式
英文:

image.Decode results in "unknown format" when source is multipart.File

问题

我有一个multipart.File,用户将其上传到我的服务器,然后我使用aws-sdk-go将该文件上传到S3,但我还想创建该图像的缩略图。

在我的测试中,以下代码在file是本地文件的os.Open(...的返回值时运行良好,但当我将相同的变量发送给s3,它会进入err块,因为s3要求一个io.Reader

import (
  "image"
  "image/jpeg"
)

func UploadToS3(file multipart.File, /*snip*/) {
  _, uploadErr := uploader.Upload(&s3manager.UploadInput{
    Bucket:      aws.String(bucket),
    Key:         aws.String(key),
    Body:        file,
    ContentType: aws.String(mimeType),
    ACL:         aws.String("public-read"),
  })
  reader, err := CreateThumbnail(file)
}

func CreateThumbnail(imageFile io.Reader) (io.Reader, error) {
  decodedImage, _, err := image.Decode(imageFile)
  if err != nil {
    fmt.Println("Error decoding", err, decodedImage) // "unknown format"
    return nil, err
  }
  /*snip*/
}

我看到的大多数答案都涉及添加import _ "image/jpeg",但已经导入了它(以及pnggif)。
我对Golang还比较陌生,所以不太清楚我做错了什么。我也尝试了image.Decode(bufio.NewReader(imageFile)),但结果是相同的err

英文:

I have a multipart.File that a user uploads to my server, and then I take that file and upload it to s3 using aws-sdk-go, but I also want to create a thumbnail of that image.

The code below works fine in my tests when I file is the return value of an os.Open(... of a local file, but it hit's the err block when I send CreateThumbnail the same variable I sent to s3, which asks for an io.Reader

import (
  "image"
  "image/jpeg"
)
func UploadToS3(file multipart.File, /*snip*/) {
  _, uploadErr := uploader.Upload(&s3manager.UploadInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
    Body:   file,
    ContentType: aws.String(mimeType),
    ACL: aws.String("public-read"),
  })
  reader, err := CreateThumbnail(file)
}    

func CreateThumbnail(imageFile io.Reader) (io.Reader, error) {
  decodedImage, _, err := image.Decode(imageFile)
  if err != nil {
   fmt.Println("Error decoding", err, decodedImage) // "unknown format"
   return nil, err
 }
 /*snip*/

Most of the answers I see for the problem involve adding import _ "image/jpeg", but that's already imported (as well as png and gif).
I'm fairly new to Golang, so I'm a bit lost as to what I'm doing wrong. I tried image.Decode(bufio.NewReader(imageFile)) as well, but that results in the same err.

答案1

得分: 8

uploader.Upload调用会读取文件直到文件末尾。在调用CreateThumbnail之前,请将文件的指针定位回文件开头:

func UploadToS3(file multipart.File, /*snip*/) {
  _, uploadErr := uploader.Upload(&s3manager.UploadInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
    Body:   file,
    ContentType: aws.String(mimeType),
    ACL: aws.String("public-read"),
  })
  // 将文件指针定位回文件开头以供CreateThumbnail使用
  if _, err := file.Seek(0, 0); err != nil {
    // 处理错误
  }
  reader, err := CreateThumbnail(file)
}
英文:

The call uploader.Upload reads to the end of the file. Seek back to the beginning of the file before calling CreateThumbnail:

func UploadToS3(file multipart.File, /*snip*/) {
  _, uploadErr := uploader.Upload(&s3manager.UploadInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
    Body:   file,
    ContentType: aws.String(mimeType),
    ACL: aws.String("public-read"),
  })
  // Seek back to beginning of file for CreateThumbnail
  if _, err := file.Seek(0, 0); err != nil {
    // handle error
  }
  reader, err := CreateThumbnail(file)
}    

huangapple
  • 本文由 发表于 2015年6月17日 23:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/30896117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定