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

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

Golang opening external files sent over http request with new package

问题

我正在使用Golang作为一个restful api。我有一个IOS应用程序,用户上传照片后,图像被发送到Golang进行处理并上传(到Amazon S3),因此这些图像在我的本地目录中找不到。我之前使用FormFile来读取图像,例如:

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
    }

    // 上传图像到我的Amazon S3存储桶的其余代码,没有问题
}

上述代码的问题是有时会上传大小为4MB的图像,这太大了,所以我使用了一个新的包来调整图像大小https://www.github.com/disintegration/imaging。我昨天在这个问题上发了帖子,得到了很好的帮助,并成功上传了在我的本地目录中找到的图像,但是我需要找到如何加载外部的图像。我遇到的唯一问题是这个包的Open方法,根据go-docs的解释,这是该方法的说明:

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

Open从文件中加载图像

所以如果我有一个在本地目录中的图像,并且想要调整它的大小,我可以这样做:

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)

// 其余代码将新图像发送到Amazon S3,它可以正常工作

这一切都正常工作,但正如前面所说,这些图像是来自其他人的IOS设备,所以我不能提供图像路径。我尝试过这样做:

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

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

但它们都显示找不到图像或目录。我对Golang还不熟悉,但是否有办法将外部发送的文件传递给imaging.Open方法?我需要这样做,因为imaging.Resize()方法的第一个参数是image.Image类型,与imaging.Open的返回类型相同。不幸的是,r.FormFile("file")的类型是Multipart.File,至少在我有限的知识中无法调整大小。任何帮助将是非常好的。

英文:

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

得分: 1

不需要打开图像;它已经在请求中可用。

你可以按照以下方式进行操作:

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

Request.FormFile 返回一个 multipart.File,它是一个 io.Reader,因此可以传递给 image.Decode 来获取一个图像,然后传递给 imaging.Resize

记得检查和处理每一个潜在的错误。

英文:

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.

huangapple
  • 本文由 发表于 2017年1月15日 04:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/41654800.html
匿名

发表评论

匿名网友

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

确定