Add S3 configuration and presigned URL logic for file upload and download
This commit is contained in:
@@ -2,10 +2,12 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -25,7 +27,29 @@ func NewDownloadPresignedURLLogic(ctx context.Context, svcCtx *svc.ServiceContex
|
||||
}
|
||||
|
||||
func (l *DownloadPresignedURLLogic) DownloadPresignedURL(req *types.DownloadPresignedURLReq) (resp *types.DownloadPresignedURLResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// Calculate expiration time
|
||||
expiration := time.Duration(l.svcCtx.Config.S3.PresignedURLExpiration) * time.Second
|
||||
expireAt := time.Now().Add(expiration).Unix()
|
||||
|
||||
return
|
||||
// Create presigned client
|
||||
presignClient := s3.NewPresignClient(l.svcCtx.S3Client)
|
||||
|
||||
// Generate presigned GET URL
|
||||
getObjectInput := &s3.GetObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3.Bucket,
|
||||
Key: &req.File_key,
|
||||
}
|
||||
|
||||
presignedReq, err := presignClient.PresignGetObject(l.ctx, getObjectInput, func(opts *s3.PresignOptions) {
|
||||
opts.Expires = expiration
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("Failed to generate presigned URL: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.DownloadPresignedURLResp{
|
||||
Url: presignedReq.URL,
|
||||
Expire_at: expireAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user