Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert *multipart.FileHeader file type to *os.File in golang

Tags:

go

amazon-s3

I want to stream multipart file directly to AWSS3 instead of saving file to disk at first. Receiving file of type *multipart.FileHeader in my golang REST API. Now I want to stream this to AWSS3, using s3manager upload method where it's required to pass *bytes.Reader in body param. I have seen an example of uploading file to AWSS3, written in a way that opening a file os.Open("filepath") of type *os.File from disk,getting *bytes.Reader out of it and passing to AWSS3. Now I am struggling to convert this *multipart.FileHeader to *os.File.

like image 277
RT Bathula Avatar asked Dec 04 '16 06:12

RT Bathula


2 Answers

Call Open on the multipart.FileHeader. It will return a multipart.File which will provide a reader to the file (which will be held in memory).

like image 118
djd Avatar answered Oct 01 '22 15:10

djd


you can use os.Open() to convert multipart.FileHeader to *(os.File)

example

_, header , _ := c.Request.FormFile("upload")
out, _ := os.Open(header.Filename)
like image 38
panapol-p Avatar answered Oct 01 '22 14:10

panapol-p