Browse Source

init

master
黄梓健 5 years ago
commit
518fb5a27f
  1. 5
      .gitignore
  2. 21
      config/config.go
  3. 38
      controller/upload.go
  4. 20
      helpers/result.go
  5. 12
      helpers/utils.go
  6. 16
      helpers/utils_test.go
  7. 13
      main.go
  8. 35
      service/bucket/upload.go

5
.gitignore

@ -0,0 +1,5 @@
.idea
go.mod
go.sum
main
main.exe

21
config/config.go

@ -0,0 +1,21 @@
package config
type Config struct {
AwsAccessKey string
AwsSecretKey string
AwsBucketName string
AwsUploadPath string
AwsRegion string
}
//access_key := "AKIAJGSRUU3DWHVGWIHA"
//secret_key := "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"
func GetConfig() *Config {
return &Config{
AwsAccessKey: "AKIAJGSRUU3DWHVGWIHA",
AwsSecretKey: "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",
AwsBucketName: "oceanapps",
AwsUploadPath: "upload/",
AwsRegion:"ap-east-1",
}
}

38
controller/upload.go

@ -0,0 +1,38 @@
package controller
import (
"aws_service/config"
"aws_service/helpers"
"aws_service/service/bucket"
"fmt"
"path/filepath"
"time"
"github.com/gin-gonic/gin"
)
func Upload(ctx *gin.Context) {
f, err := ctx.FormFile("file")
if err != nil {
helpers.Return(ctx, 504, "文件上传失败", err.Error())
return
}
if f == nil {
helpers.Return(ctx, 504, "文件权柄为空", nil)
return
}
cfg := config.GetConfig()
fio, err := f.Open()
if err != nil {
helpers.Return(ctx, 504, "文件上传失败", err.Error())
}
filename := fmt.Sprintf("%d%s%s", time.Now().Unix(), helpers.Md5(f.Filename), filepath.Ext(f.Filename))
uri, err := bucket.Upload(cfg.AwsAccessKey, cfg.AwsSecretKey, cfg.AwsRegion, cfg.AwsBucketName,
cfg.AwsUploadPath, filename, fio)
if err != nil {
helpers.Return(ctx, 504, "文件上传失败", err.Error())
return
}
helpers.Return(ctx, 0, "文件上传成功", uri)
}

20
helpers/result.go

@ -0,0 +1,20 @@
package helpers
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Result struct {
Code int
Msg string
Data interface{}
}
func Return(ctx *gin.Context, code int, msg string, data interface{}) {
ctx.JSON(http.StatusOK, gin.H{
"code": code,
"msg": msg,
"data":data,
})
}

12
helpers/utils.go

@ -0,0 +1,12 @@
package helpers
import (
"crypto/md5"
"encoding/hex"
)
func Md5(key string) string {
h := md5.New()
h.Write([]byte(key))
return hex.EncodeToString(h.Sum(nil))
}

16
helpers/utils_test.go

@ -0,0 +1,16 @@
package helpers
import (
"fmt"
"testing"
)
func TestMd5(t *testing.T) {
var a = Md5("hello")
var b = Md5("world")
if a == b {
t.Error("测试失败")
}
fmt.Println(a)
fmt.Println(b)
}

13
main.go

@ -0,0 +1,13 @@
package main
import (
"aws_service/controller"
"github.com/gin-gonic/gin"
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.POST("/upload", controller.Upload)
router.Run(":8080")
}

35
service/bucket/upload.go

@ -0,0 +1,35 @@
package bucket
import (
"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/s3manager"
"io"
)
func Upload(key, secret,region, bucket, path, name string, body io.Reader) (string, error) {
//access_key := "AKIAJGSRUU3DWHVGWIHA"
//secret_key := "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"
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(key, secret, ""),
Region: aws.String(region),
//DisableSSL: aws.Bool(true),
//S3ForcePathStyle: aws.Bool(false), //virtual-host style方式,不要修改
})
if err != nil {
return "", err
}
uploader := s3manager.NewUploader(sess)
out, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(path + name),
Body: body,
})
if err != nil {
return "", err
}
return out.Location, nil
}
Loading…
Cancel
Save