生命周期的指令上传
This commit is contained in:
parent
70fd711fa5
commit
975fe89eca
@ -5,6 +5,7 @@ import (
|
|||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
"fusenapi/utils/check"
|
"fusenapi/utils/check"
|
||||||
"fusenapi/utils/format"
|
"fusenapi/utils/format"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
"fusenapi/server/upload/internal/types"
|
"fusenapi/server/upload/internal/types"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/request"
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
@ -55,26 +57,61 @@ func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileB
|
|||||||
return resp.SetStatus(basic.CodeS3CategoryErr)
|
return resp.SetStatus(basic.CodeS3CategoryErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var s3req *request.Request
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
s3req, _ := svc.PutObjectRequest(
|
category := format.TypeCategory(req.Category)
|
||||||
&s3.PutObjectInput{
|
ObjectKey := aws.String(format.FormatS3KeyName(
|
||||||
Bucket: aws.String("storage.fusenpack.com"),
|
keytype,
|
||||||
Key: aws.String(format.FormatS3KeyName(
|
uid,
|
||||||
keytype,
|
now,
|
||||||
uid,
|
l.svcCtx.Config.Env,
|
||||||
now,
|
category,
|
||||||
l.svcCtx.Config.Env,
|
req.File.Filename,
|
||||||
req.Category,
|
))
|
||||||
req.File.Filename,
|
|
||||||
)),
|
switch category {
|
||||||
},
|
case format.TCategoryRenderMegre:
|
||||||
)
|
|
||||||
|
lifecycleConfiguration := &s3.BucketLifecycleConfiguration{
|
||||||
|
Rules: []*s3.LifecycleRule{
|
||||||
|
{
|
||||||
|
Status: aws.String("Enabled"),
|
||||||
|
// Status: aws.String("Disabled"),
|
||||||
|
Filter: &s3.LifecycleRuleFilter{
|
||||||
|
Prefix: ObjectKey,
|
||||||
|
},
|
||||||
|
Expiration: &s3.LifecycleExpiration{
|
||||||
|
// Date: aws.Time(time.Now().UTC().Add(time.Minute)),
|
||||||
|
Days: aws.Int64(1), // 设置过期天数,例如:30天
|
||||||
|
},
|
||||||
|
// ID: aws.String("ExpireAfter01Days"), // 设置规则的唯一标识符
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
s3req, _ = svc.PutBucketLifecycleConfigurationRequest(
|
||||||
|
&s3.PutBucketLifecycleConfigurationInput{
|
||||||
|
Bucket: basic.StorageBucketName,
|
||||||
|
LifecycleConfiguration: lifecycleConfiguration,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
s3req, _ = svc.PutObjectRequest(
|
||||||
|
&s3.PutObjectInput{
|
||||||
|
Bucket: basic.StorageBucketName,
|
||||||
|
Key: ObjectKey,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
s3req.SetBufferBody(req.File.Data)
|
s3req.SetBufferBody(req.File.Data)
|
||||||
err := s3req.Send()
|
err := s3req.Send()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logx.Error(err)
|
||||||
return resp.SetStatus(basic.CodeS3PutObjectRequestErr)
|
return resp.SetStatus(basic.CodeS3PutObjectRequestErr)
|
||||||
}
|
}
|
||||||
|
log.Println(s3req.HTTPRequest.URL)
|
||||||
|
|
||||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||||
"upload_url": s3req.HTTPRequest.URL.String(),
|
"upload_url": s3req.HTTPRequest.URL.String(),
|
||||||
|
@ -61,6 +61,7 @@ func (l *UploadFileFrontendLogic) UploadFileFrontend(req *types.RequestUploadFil
|
|||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
s3req, _ := svc.PutObjectRequest(
|
s3req, _ := svc.PutObjectRequest(
|
||||||
&s3.PutObjectInput{
|
&s3.PutObjectInput{
|
||||||
Bucket: aws.String("storage.fusenpack.com"),
|
Bucket: aws.String("storage.fusenpack.com"),
|
||||||
@ -69,7 +70,7 @@ func (l *UploadFileFrontendLogic) UploadFileFrontend(req *types.RequestUploadFil
|
|||||||
uid,
|
uid,
|
||||||
now,
|
now,
|
||||||
l.svcCtx.Config.Env,
|
l.svcCtx.Config.Env,
|
||||||
req.Category,
|
format.TypeCategory(req.Category),
|
||||||
req.FileName,
|
req.FileName,
|
||||||
)),
|
)),
|
||||||
ContentLength: aws.Int64(req.FileSize),
|
ContentLength: aws.Int64(req.FileSize),
|
||||||
|
56
server/upload/test/uploadfilebackendhandler_test.go
Normal file
56
server/upload/test/uploadfilebackendhandler_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"fusenapi/utils/format"
|
||||||
|
fstests "fusenapi/utils/fstests"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/474420502/requests"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCaseRenderMegre(t *testing.T) {
|
||||||
|
var err error
|
||||||
|
var resp *requests.Response
|
||||||
|
var result gjson.Result
|
||||||
|
|
||||||
|
// 获取 session,并携带 JWT token
|
||||||
|
ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port)
|
||||||
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/upload/upload-file-backend", cnf.Host, cnf.Port))
|
||||||
|
|
||||||
|
// data, err := ioutil.ReadFile("./fusen.webp")
|
||||||
|
// if err != nil {
|
||||||
|
// t.Error(err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
mp := tp.CreateBodyMultipart()
|
||||||
|
mp.WriteField("file_type", "image")
|
||||||
|
mp.WriteField("category", string(format.TCategoryRenderMegre))
|
||||||
|
mp.CreateFormFile("file", "./fusen.web")
|
||||||
|
tp.SetBodyFormData(mp)
|
||||||
|
|
||||||
|
// 向服务器发送 GET 请求,获取用户基本信息
|
||||||
|
resp, err = tp.TestExecute(gserver)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = resp.Json()
|
||||||
|
if result.Get("code").Int() != 200 {
|
||||||
|
t.Error("code != 200")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !result.Get("data").Exists() {
|
||||||
|
t.Error("data not exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !result.Get("data.upload_url").Exists() {
|
||||||
|
t.Error("upload_url not exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetStatusCode() != 200 {
|
||||||
|
t.Error("put failed")
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCaseUploadfileFrontend(t *testing.T) {
|
func TestCasePersonalization(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
var resp *requests.Response
|
var resp *requests.Response
|
||||||
var result gjson.Result
|
var result gjson.Result
|
||||||
|
@ -6,9 +6,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 全局的BucketName
|
||||||
|
var StorageBucketName = aws.String("storage.fusenpack.com")
|
||||||
|
|
||||||
const UploadFileLimitSize = 200 << 20
|
const UploadFileLimitSize = 200 << 20
|
||||||
|
|
||||||
// File uploadfile 文件(multipart...)
|
// File uploadfile 文件(multipart...)
|
||||||
|
@ -52,10 +52,21 @@ func FormatS3KeyNameUser(userid int64, now time.Time, env string, category TypeC
|
|||||||
|
|
||||||
switch category {
|
switch category {
|
||||||
case TCategoryPersonalization:
|
case TCategoryPersonalization:
|
||||||
// TODO:
|
// /{env}/personalization/{userid}/{filename}
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%s.%s", env, category, userid, name, ext)
|
||||||
|
case TCategoryRenderMegre:
|
||||||
|
// /{env}/render_megre/{userid}/{filename} 自动删除
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%s_%d.%s", env, category, userid, name, now.Unix(), ext)
|
||||||
|
case TCategory3DTools:
|
||||||
|
// /{env}/3dtools/年月/{userid}/{filename}
|
||||||
|
return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), userid, name, now.Unix(), ext)
|
||||||
|
case TCategoryQuotation:
|
||||||
|
// /{env}/quotation/年月/{userid}/{filename}
|
||||||
|
return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), userid, name, now.Unix(), ext)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, userid, year, int(month), name, now.Unix(), ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, userid, year, int(month), name, now.Unix(), ext)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatS3KeyNameGuest 游客的格式化存储
|
// FormatS3KeyNameGuest 游客的格式化存储
|
||||||
@ -69,5 +80,24 @@ func FormatS3KeyNameGuest(guestid int64, now time.Time, env string, category Typ
|
|||||||
name = names[0]
|
name = names[0]
|
||||||
ext = names[1]
|
ext = names[1]
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("/%s/guest/%s/%d/%04d%02d/%s_%d.%s", env, category, guestid, year, int(month), name, now.Unix(), ext)
|
|
||||||
|
switch category {
|
||||||
|
case TCategoryPersonalization:
|
||||||
|
// /{env}/personalization/{guestid}/{filename}
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%s.%s", env, category, guestid, name, ext)
|
||||||
|
case TCategoryRenderMegre:
|
||||||
|
// /{env}/render_megre/{guestid}/{filename} 自动删除
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%s_%d.%s", env, category, guestid, name, now.Unix(), ext)
|
||||||
|
case TCategory3DTools:
|
||||||
|
panic(TCategory3DTools + "不存在游客")
|
||||||
|
// /{env}/3dtools/年月/{guestid}/{filename}
|
||||||
|
// return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), guestid, name, now.Unix(), ext)
|
||||||
|
case TCategoryQuotation:
|
||||||
|
panic(TCategoryQuotation + "不存在游客")
|
||||||
|
// /{env}/quotation/年月/{guestid}/{filename}
|
||||||
|
// return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), guestid, name, now.Unix(), ext)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, guestid, year, int(month), name, now.Unix(), ext)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user