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

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

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

问题

我是新手使用Golang,我有一个函数可以将图像上传到我的Amazon S3账户并保存。一切都很顺利,但是我想找到一种方法来调整图像的大小,因为有些图像大小高达4MB。我找到了一个可以正确调整图像大小的包https://github.com/disintegration/imaging。我的问题是,我如何使用新调整大小的图像而不是旧文件进行上传?例如,这是我的原始代码,用于上传图像而不调整大小:

  1. func UploadProfile(w http.ResponseWriter, r *http.Request) {
  2. r.ParseForm()
  3. var buff bytes.Buffer
  4. var result string
  5. sess, _ := "access keys"
  6. svc := s3.New(sess)
  7. file, handler, err := r.FormFile("file")
  8. if err != nil {
  9. fmt.Println("Error Uploading Image")
  10. return
  11. }
  12. defer file.Close()
  13. read_file,err := ioutil.ReadAll(file)
  14. fileBytes := bytes.NewReader(read_file)
  15. if err != nil {
  16. fmt.Println("Error Reading file")
  17. return
  18. }
  19. file.Read(read_file)
  20. path := handler.Filename
  21. params := &s3.PutObjectInput{
  22. Bucket: aws.String("amazon-bucket"),
  23. Key: aws.String(path),
  24. Body: fileBytes,
  25. }
  26. resp, err := svc.PutObject(params)
  27. if err != nil {
  28. fmt.Printf("bad response: %s", err)
  29. }
  30. }

我现在遇到的问题是下面的代码片段:

  1. file_open,err := imaging.Open("myImage.jpg")
  2. if err != nil {
  3. print("Imaging Open error")
  4. }
  5. new_image := imaging.Resize(file_open, 300, 300, imaging.Lanczos)
  6. errs := imaging.Save(new_image, "dst.jpg")
  7. if errs != nil {
  8. print(err)
  9. }

上面的代码获取任何图像并将其调整为300x300,这正是我想要的,现在我如何使用这个新图像并将其发送到我的S3图像存储桶?让我困惑的部分是,new_image的类型是image.NRGBA,而原始图像的类型是Multipart.File。当我将图像上传到Amazon时,它想要知道图像有多少字节,我不能像下面这样做:

  1. read_file,err := ioutil.ReadAll(new_image)
  2. if err != nil {
  3. fmt.Println("Error Reading file")
  4. return
  5. }

因为它只接受io.Reader类型的参数,有没有解决这个问题的方法?这样我就可以将新图像保存到Amazon S3了。

英文:

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

  1. func UploadProfile(w http.ResponseWriter, r *http.Request) {
  2. r.ParseForm()
  3. var buff bytes.Buffer
  4. var result string
  5. sess, _ := "access keys"
  6. svc := s3.New(sess)
  7. file, handler, err := r.FormFile("file")
  8. if err != nil {
  9. fmt.Println("Error Uploading Image")
  10. return
  11. }
  12. defer file.Close()
  13. read_file,err := ioutil.ReadAll(file)
  14. fileBytes := bytes.NewReader(read_file)
  15. if err != nil {
  16. fmt.Println("Error Reading file")
  17. return
  18. }
  19. file.Read(read_file)
  20. path := handler.Filename
  21. params := &s3.PutObjectInput{
  22. Bucket: aws.String("amazon-bucket"),
  23. Key: aws.String(path),
  24. Body: fileBytes,
  25. }
  26. resp, err := svc.PutObject(params)
  27. if err != nil {
  28. fmt.Printf("bad response: %s", err)
  29. }
  30. }

The issue I am now having is with this snippet below

  1. file_open,err := imaging.Open("myImage.jpg")
  2. if err != nil {
  3. print("Imaging Open error")
  4. }
  5. new_image := imaging.Resize(file_open, 300, 300, imaging.Lanczos)
  6. errs := imaging.Save(new_image, "dst.jpg")
  7. if errs != nil {
  8. print(err)
  9. }

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

  1. read_file,err := ioutil.ReadAll(new_image)
  2. if err != nil {
  3. fmt.Println("Error Reading file")
  4. return
  5. }

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 ?

答案1

得分: 2

你正在将新图像保存到文件中,然后没有对该文件进行任何操作。如果你想要的话,可以从磁盘中读取该文件,或者可以在内存中对其进行编码:

  1. ...
  2. resized := imaging.Resize(fileOpen, 300, 300, imaging.Lanczos)
  3. var buf bytes.Buffer
  4. err = imaging.Encode(&buf, resized, imaging.JPEG)
  5. if err != nil {
  6. log.Println(err)
  7. return
  8. }
  9. params := &s3.PutObjectInput{
  10. Bucket: aws.String("amazon-bucket"),
  11. Key: aws.String(path),
  12. Body: buf.Bytes(),
  13. }
  14. ...
英文:

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:

  1. ...
  2. resized := imaging.Resize(fileOpen, 300, 300, imaging.Lanczos)
  3. var buf byte.Buffer
  4. err = imaging.Encode(&buf, resized, imaging.JPEG)
  5. if err != nil {
  6. log.Println(err)
  7. return
  8. }
  9. params := &s3.PutObjectInput{
  10. Bucket: aws.String("amazon-bucket"),
  11. Key: aws.String(path),
  12. Body: buf.Bytes(),
  13. }
  14. ...

huangapple
  • 本文由 发表于 2017年1月14日 06:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/41644747.html
匿名

发表评论

匿名网友

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

确定