多文件上传--前端
This commit is contained in:
parent
4a2a7822b7
commit
a0ed98d2d1
@ -80,6 +80,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
|
|||||||
}
|
}
|
||||||
//设置内存大小
|
//设置内存大小
|
||||||
l.r.ParseMultipartForm(32 << 20)
|
l.r.ParseMultipartForm(32 << 20)
|
||||||
|
|
||||||
//获取上传的文件组
|
//获取上传的文件组
|
||||||
files := l.r.MultipartForm.File["file"]
|
files := l.r.MultipartForm.File["file"]
|
||||||
|
|
||||||
@ -117,6 +118,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
|
|||||||
Metadata: info.Metadata,
|
Metadata: info.Metadata,
|
||||||
FileData: ioData,
|
FileData: ioData,
|
||||||
ApiType: req.ApiType,
|
ApiType: req.ApiType,
|
||||||
|
Bucket: uploadBucket,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, func(item interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
}, func(item interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
||||||
@ -135,7 +137,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
|
|||||||
// 创建S3对象存储请求
|
// 创建S3对象存储请求
|
||||||
s3req, _ = svc.PutObjectRequest(
|
s3req, _ = svc.PutObjectRequest(
|
||||||
&s3.PutObjectInput{
|
&s3.PutObjectInput{
|
||||||
Bucket: &uploadBucket,
|
Bucket: &uploadDataInfo.Bucket,
|
||||||
Key: &uploadDataInfo.FileKey,
|
Key: &uploadDataInfo.FileKey,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -211,9 +213,11 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
|
|||||||
|
|
||||||
type uploadData struct {
|
type uploadData struct {
|
||||||
ApiType int64 `json:"api_type"`
|
ApiType int64 `json:"api_type"`
|
||||||
|
FileSize int64 `json:"file_size"`
|
||||||
FileType string `json:"file_type"`
|
FileType string `json:"file_type"`
|
||||||
FileKey string `json:"file_key"`
|
FileKey string `json:"file_key"`
|
||||||
Metadata string `json:"metadata"`
|
Metadata string `json:"metadata"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
FileData []byte `fsfile:"data"`
|
FileData []byte `fsfile:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,13 +3,17 @@ package logic
|
|||||||
import (
|
import (
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fusenapi/server/upload/internal/svc"
|
"fusenapi/server/upload/internal/svc"
|
||||||
"fusenapi/server/upload/internal/types"
|
"fusenapi/server/upload/internal/types"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/core/mr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UploadFilesFrontendLogic struct {
|
type UploadFilesFrontendLogic struct {
|
||||||
@ -39,7 +43,103 @@ func (l *UploadFilesFrontendLogic) UploadFilesFrontend(req *types.UploadFilesReq
|
|||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
// userinfo 传入值时, 一定不为null
|
// userinfo 传入值时, 一定不为null
|
||||||
|
|
||||||
// 检查用户是否是旁观者,旁观者没有文件上传权限
|
// 定义用户ID和S3键名格式
|
||||||
|
// var userId int64
|
||||||
|
// var guestId int64
|
||||||
|
|
||||||
return resp.SetStatus(basic.CodeOK)
|
// 检查用户是否是游客
|
||||||
|
// if userinfo.IsGuest() {
|
||||||
|
// // 如果是,使用游客ID和游客键名格式
|
||||||
|
// guestId = userinfo.GuestId
|
||||||
|
// } else {
|
||||||
|
// // 否则,使用用户ID和用户键名格式
|
||||||
|
// userId = userinfo.UserId
|
||||||
|
// }
|
||||||
|
|
||||||
|
var aa = make([]types.UploadInfo, 2)
|
||||||
|
aa[0].FileKeys = "202308011632"
|
||||||
|
aa[1].FileKeys = "202308011633"
|
||||||
|
req.UploadInfo = aa
|
||||||
|
var fileLen = len(req.UploadInfo)
|
||||||
|
|
||||||
|
if fileLen == 0 {
|
||||||
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
|
||||||
|
}
|
||||||
|
if req.ApiType == 1 && fileLen > 100 {
|
||||||
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err, files count is beyond the maximum")
|
||||||
|
}
|
||||||
|
|
||||||
|
var uploadBucket = req.UploadBucket
|
||||||
|
|
||||||
|
// 设置AWS会话的区域
|
||||||
|
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
||||||
|
|
||||||
|
// 创建新的S3服务实例
|
||||||
|
svc := s3.New(l.svcCtx.AwsSession)
|
||||||
|
|
||||||
|
result, err := mr.MapReduce(func(source chan<- interface{}) {
|
||||||
|
for _, info := range req.UploadInfo {
|
||||||
|
if info.FileSize <= 1024*1024*500 {
|
||||||
|
// 一系列业务逻辑....验证类型,文件大小
|
||||||
|
var fileKey string = info.FileKeys
|
||||||
|
source <- uploadData{
|
||||||
|
FileKey: fileKey,
|
||||||
|
FileSize: info.FileSize,
|
||||||
|
Bucket: uploadBucket,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, func(item interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
||||||
|
var uploadUrl = uploadUrl{}
|
||||||
|
uploadDataInfo := item.(uploadData)
|
||||||
|
|
||||||
|
s3req, _ := svc.PutObjectRequest(
|
||||||
|
&s3.PutObjectInput{
|
||||||
|
Bucket: &uploadBucket,
|
||||||
|
Key: &uploadDataInfo.FileKey,
|
||||||
|
ContentLength: aws.Int64(uploadDataInfo.FileSize),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
url, err := s3req.Presign(time.Minute * 5)
|
||||||
|
if err != nil {
|
||||||
|
logx.Error(err)
|
||||||
|
uploadUrl.Status = 0
|
||||||
|
uploadUrl.Url = ""
|
||||||
|
uploadUrl.Key = uploadDataInfo.FileKey
|
||||||
|
} else {
|
||||||
|
// 打印请求URL
|
||||||
|
logx.Info(url)
|
||||||
|
uploadUrl.Status = 1
|
||||||
|
uploadUrl.Url = url
|
||||||
|
uploadUrl.Key = uploadDataInfo.FileKey
|
||||||
|
}
|
||||||
|
// Notice 这个必须加!
|
||||||
|
writer.Write(uploadUrl)
|
||||||
|
}, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
||||||
|
var uploadUrlList = make(map[string][]*uploadUrl)
|
||||||
|
var uploadUrlListFail []*uploadUrl
|
||||||
|
var uploadUrlListSuccess []*uploadUrl
|
||||||
|
for p := range pipe {
|
||||||
|
var uploadUrl = p.(uploadUrl)
|
||||||
|
if uploadUrl.Status == 1 {
|
||||||
|
uploadUrlListSuccess = append(uploadUrlListSuccess, &uploadUrl)
|
||||||
|
} else {
|
||||||
|
uploadUrlListFail = append(uploadUrlListFail, &uploadUrl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notice 这个必须加!
|
||||||
|
uploadUrlList["success"] = uploadUrlListSuccess
|
||||||
|
uploadUrlList["fail"] = uploadUrlListFail
|
||||||
|
writer.Write(uploadUrlList)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logx.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回成功的响应和上传URL
|
||||||
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||||
|
"upload_urls": result,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UploadInfo struct {
|
type UploadInfo struct {
|
||||||
|
FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息
|
||||||
FileKeys string `form:"file_keys,optional"` // 上传唯一标识信息
|
FileKeys string `form:"file_keys,optional"` // 上传唯一标识信息
|
||||||
Metadata string `form:"file_keys,optional"` // 上传文件额外信息
|
Metadata string `form:"file_keys,optional"` // 上传文件额外信息
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ service upload {
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
UploadInfo {
|
UploadInfo {
|
||||||
|
FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息
|
||||||
FileKeys string `form:"file_keys,optional"` // 上传唯一标识信息
|
FileKeys string `form:"file_keys,optional"` // 上传唯一标识信息
|
||||||
Metadata string `form:"file_keys,optional"` // 上传文件额外信息
|
Metadata string `form:"file_keys,optional"` // 上传文件额外信息
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user