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

Issue

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.

Solution

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

Answered By – djd

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.