如何使用gin框架将文件上传到Amazon S3

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?

查看全部
dongtou5557
dongtou5557
2018/01/12 07:44
  • file-upload
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复