douguanya6399 2017-01-14 20:43
浏览 64
已采纳

Golang使用新包打开通过HTTP请求发送的外部文件

I am using Golang as a restful api. I have an IOS app that user's upload photos afterwards the image is sent to Golang and it processes the image and uploads it (to Amazon s3), therefore the images are not found in my local directory . I was using the FormFile before to read the images such as

func s3ImageUpload(w http.ResponseWriter, r *http.Request) {
     r.ParseForm()

     file, handler, err := r.FormFile("file")
     if err != nil {
         fmt.Println("Error Uploading Image")
         return
     }

  // Rest of Code that uploads Image to my Amazon s3 Bucket no issues here
}

The problem with the above code is that sometimes Images as big as 4MB were being uploaded and that was too big so I got a new package to resize images https://www.github.com/disintegration/imaging . I posted on this yesterday and received great help and managed to upload images found on my local directory, however I need to find how to load images coming externally . The only issue I been having is with the Open method of this package and according to go-docs this is the explanation of that method

func Open

func Open(filename string) (image.Image, error)

Open loads an image from file

so if I have an image in my local directory and want to resize it I can do this

file_open,err := imaging.Open("/Users/IH/Documents/pictures/BigTurtle.jpg")

        if err != nil {
            print("Imaging Open error")
        }
        new_image := imaging.Resize(file_open, 300, 300, imaging.Lanczos)

// rest of code sends new image to Amazon s3 it works correctly That all works but as stated before the images are coming externally from other people's IOS devices so I can not put in an image path. I have tried to do this

file_open,err := imaging.Open(handler.filename)

and

 file_open,err := imaging.Open("file")

but it states no image or directory found, I am new to Golang but is there some way that I can get the external file being sent into imaging.open ? I need that one because the 1st parameter of the method `imaging.Resize() is of type image.Image which is the same type as imaging.Open . Unfortunately the r.FormFile("file") is of type Multipart.File and can not be resized at least not to my limited knowledge. Any help would be great .

  • 写回答

1条回答 默认 最新

  • dongyin5516 2017-01-14 22:20
    关注

    No need to open the image; it's already available on the request.

    You can do something along these lines:

    file = r.FormFile()
    img = image.Decode(file)
    imgResized = imaging.Resize(img, ..)
    

    Request.FormFile returns a multipart.File, which is an io.Reader, so can be passed to image.Decode to get an Image to pass to imaging.Resize.

    Remember to check and handle every potential error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容