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

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

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

问题

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

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)

    }

}

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

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)
}

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

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

因为它只接受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

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

得分: 2

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

...
resized := imaging.Resize(fileOpen, 300, 300, imaging.Lanczos)

var buf bytes.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(),
}
...
英文:

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(),
}
...

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:

确定