Skip to content

Commit

Permalink
fix: none content-type for the object
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed May 17, 2020
1 parent fed5e5a commit 9b1e93e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 8 additions & 5 deletions uploader/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"

"uptoc/utils"
)

// S3Uploader implements the Driver base on the S3 of AWS.
Expand Down Expand Up @@ -65,16 +67,17 @@ func (u *S3Uploader) ListObjects() ([]Object, error) {

// Upload uploads the local file to the object
func (u *S3Uploader) Upload(objectKey, filePath string) (err error) {
body, err := os.Open(filePath)
bodyReader, err := os.Open(filePath)
if err != nil {
return err
}
defer body.Close()
defer bodyReader.Close()

_, err = u.client.PutObject(&s3.PutObjectInput{
Body: body,
Bucket: aws.String(u.bucket),
Key: aws.String(objectKey),
Body: bodyReader,
Bucket: aws.String(u.bucket),
Key: aws.String(objectKey),
ContentType: aws.String(utils.FileContentType(filePath)),
})
return
}
Expand Down
12 changes: 12 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/md5"
"encoding/hex"
"io"
"io/ioutil"
"net/http"
"os"
)

Expand All @@ -22,3 +24,13 @@ func FileMD5(filepath string) string {

return hex.EncodeToString(md5hash.Sum(nil)[:])
}

// FileContentType returns the file content-type
func FileContentType(filepath string) string {
fileData, err := ioutil.ReadFile(filepath)
if err != nil {
return ""
}

return http.DetectContentType(fileData)
}

0 comments on commit 9b1e93e

Please sign in to comment.