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

报告相同问题?

悬赏问题

  • ¥15 基于OPENCV的人脸识别
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量