Prashanth Hegde
Prashanth Hegde

Reputation: 174

How to upload file to amazon s3 using gin framework

I am trying to upload a file to Amazon S3 Using gin framework of Go. Since aws-sdc requires to read the file i need to open file using os.open('filename').But since i am getting the file from "formFile" I don't have the path of the file to open, so os.Open() is giving error

The system cannot find the file specified.

My approach is as follows

package controllers

import (
    "bytes"
    "log"
    "net/http"
    "os"

    "github.com/gin-gonic/gin"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

const (
    S3_REGION = "MY REGION"
    S3_BUCKET = "<MY BUCKET>"
)

func UploadDocument(c *gin.Context) {
    var Buf bytes.Buffer
    file, _ := c.FormFile("file")

    creds := credentials.NewSharedCredentials("", "default")

    s, err := session.NewSession(&aws.Config{
        Region:      aws.String(S3_REGION),
        Credentials: creds,
    })
    if err != nil {
        log.Fatal(err)
    }

    err = AddFilesToS3(s, file.fileName)
    if err != nil {
        log.Fatal(err)
    }
}

func AddFilesToS3(s *session.Session, fileDir string) error {
    file, err := os.Open(fileDir)
    if err != nil {
        return err
    }
    defer file.Close()

    fileInfo, _ := file.Stat()
    var size int64 = fileInfo.Size()
    buffer := make([]byte, size)
    file.Read(buffer)

    _, err = s3.New(s).PutObject(&s3.PutObjectInput{
        Bucket:               aws.String(S3_BUCKET),
        Key:                  aws.String("myfolder" + "/" + fileDir),
        ACL:                  aws.String("private"),
        Body:                 bytes.NewReader(buffer),
        ContentLength:        aws.Int64(size),
        ContentType:          aws.String(http.DetectContentType(buffer)),
        ContentDisposition:   aws.String("attachment"),
        ServerSideEncryption: aws.String("AES256"),
    })
    return err

}

I am sending my file through POSTMAN like this

enter image description here

what I need to pass to my 'AddFilesToS3' function, since I am sending just the file name, os.Open(fileDir) is failing to look to the actual path of the file. Where am I doing wrong or is there any better method available to do this?

Upvotes: 1

Views: 5563

Answers (1)

Marc
Marc

Reputation: 21055

You're not even reading the file from the form.

You need to call FileHeader.Open. This returns a multipart.File which implements the standard io.Reader interface.

You should change AddFilesToS3 to take a filename and io.Reader. This can then be called for files from gin as well as regular files.

  fileHeader, err := c.FormFile("file")
  // check err
  f, err := fileHeader.Open()
  // check err
  err = AddFilesToS3(s, fileHeader.fileName, f)

And your AddFilesToS3 is now:

func AddFilesToS3(s *session.Session, fileDir string, r io.Reader) error {
  // left as an exercise for the OP

You may need to pass fileHeader.Size() as well.

Upvotes: 6

Related Questions