|
|
@ -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) |
|
|
|
} |