dtip91401 2017-01-13 22:57
浏览 84
已采纳

Golang如何将新软件包集成到我的图片上传代码中

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 ?

  • 写回答

1条回答 默认 最新

  • douchan6512 2017-01-13 23:28
    关注

    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(),
    }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!