fix: api descript
This commit is contained in:
@@ -1,7 +1,19 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
S3Conf S3ObjectConf
|
||||
}
|
||||
|
||||
type S3ObjectConf struct {
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
Bucket string
|
||||
Endpoint string
|
||||
Region string
|
||||
UsePathStyle bool
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/objectstory/rpc/internal/svc"
|
||||
"juwan-backend/app/objectstory/rpc/pb"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -25,7 +28,26 @@ func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFil
|
||||
|
||||
// 获取文件访问链接(处理私有文件的鉴权)
|
||||
func (l *GetFileUrlLogic) GetFileUrl(in *pb.GetFileUrlReq) (*pb.GetFileUrlResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if in == nil || in.GetFileId() == "" {
|
||||
return nil, errors.New("file id is required")
|
||||
}
|
||||
|
||||
return &pb.GetFileUrlResp{}, nil
|
||||
_, err := l.svcCtx.S3.HeadObject(l.ctx, &s3.HeadObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &in.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
presignClient := s3.NewPresignClient(l.svcCtx.S3)
|
||||
presigned, err := presignClient.PresignGetObject(l.ctx, &s3.GetObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &in.FileId,
|
||||
}, s3.WithPresignExpires(15*time.Minute))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.GetFileUrlResp{Url: presigned.URL}, nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/objectstory/rpc/internal/svc"
|
||||
"juwan-backend/app/objectstory/rpc/pb"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -25,7 +33,40 @@ func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogi
|
||||
|
||||
// 简单上传(适合小文件,或保存元数据)
|
||||
func (l *UploadLogic) Upload(in *pb.UploadFileMetadataReq) (*pb.UploadFileResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if in == nil {
|
||||
return nil, errors.New("invalid request")
|
||||
}
|
||||
if len(in.GetFileData()) == 0 {
|
||||
return nil, errors.New("file data is required")
|
||||
}
|
||||
if in.GetFileName() == "" {
|
||||
return nil, errors.New("file name is required")
|
||||
}
|
||||
if in.GetFileType() == "" {
|
||||
return nil, errors.New("file type is required")
|
||||
}
|
||||
|
||||
return &pb.UploadFileResp{}, nil
|
||||
fileID := uuid.NewString()
|
||||
ext := strings.ToLower(filepath.Ext(in.GetFileName()))
|
||||
if len(ext) > 10 {
|
||||
ext = ""
|
||||
}
|
||||
objectKey := fmt.Sprintf("%s/%s/%s%s", in.GetFileType(), in.GetUserId(), fileID, ext)
|
||||
|
||||
_, err := l.svcCtx.S3.PutObject(l.ctx, &s3.PutObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &objectKey,
|
||||
Body: bytes.NewReader(in.GetFileData()),
|
||||
ContentLength: aws.Int64(int64(len(in.GetFileData()))),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := strings.TrimRight(l.svcCtx.Config.S3Conf.Endpoint, "/") + "/" + l.svcCtx.Config.S3Conf.Bucket + "/" + objectKey
|
||||
|
||||
return &pb.UploadFileResp{
|
||||
Url: url,
|
||||
FileId: objectKey,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/objectstory/rpc/internal/config"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awsConfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type ObjectStorageSvc struct {
|
||||
c config.S3ObjectConf
|
||||
}
|
||||
|
||||
func NewObjectStorage(c config.S3ObjectConf) *ObjectStorageSvc {
|
||||
return &ObjectStorageSvc{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ObjectStorageSvc) MustNews3Client(ctx context.Context) *s3.Client {
|
||||
awsCfg, err := awsConfig.LoadDefaultConfig(ctx,
|
||||
awsConfig.WithRegion(s.c.Region),
|
||||
awsConfig.WithCredentialsProvider(
|
||||
credentials.NewStaticCredentialsProvider(
|
||||
s.c.AccessKey,
|
||||
s.c.SecretKey,
|
||||
"",
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return s3.NewFromConfig(awsCfg, func(o *s3.Options) {
|
||||
o.BaseEndpoint = aws.String(s.c.Endpoint)
|
||||
o.UsePathStyle = s.c.UsePathStyle
|
||||
})
|
||||
}
|
||||
@@ -1,13 +1,22 @@
|
||||
package svc
|
||||
|
||||
import "juwan-backend/app/objectstory/rpc/internal/config"
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/objectstory/rpc/internal/config"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
S3 *s3.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
s3obj := NewObjectStorage(c.S3Conf)
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
S3: s3obj.MustNews3Client(context.Background()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user