commit 518fb5a27fa228d6747f7887afbeaa0484230da0 Author: tommy <3405129587@qq.com> Date: Thu Mar 12 19:09:34 2020 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f157dd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +go.mod +go.sum +main +main.exe \ No newline at end of file diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..a8d26d5 --- /dev/null +++ b/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", + } +} diff --git a/controller/upload.go b/controller/upload.go new file mode 100644 index 0000000..3625d12 --- /dev/null +++ b/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) +} diff --git a/helpers/result.go b/helpers/result.go new file mode 100644 index 0000000..fd62f30 --- /dev/null +++ b/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, + }) +} diff --git a/helpers/utils.go b/helpers/utils.go new file mode 100644 index 0000000..c87d47e --- /dev/null +++ b/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)) +} diff --git a/helpers/utils_test.go b/helpers/utils_test.go new file mode 100644 index 0000000..3d89ab3 --- /dev/null +++ b/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) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..77b575c --- /dev/null +++ b/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") +} diff --git a/service/bucket/upload.go b/service/bucket/upload.go new file mode 100644 index 0000000..47c575a --- /dev/null +++ b/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 +}