英文:
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"
,但已经导入了它(以及png
和gif
)。
我对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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论