Add S3 configuration and presigned URL logic for file upload and download

This commit is contained in:
2025-10-24 22:32:36 +08:00
parent 289812aac3
commit ec80aa92fa
8 changed files with 225 additions and 11 deletions

View File

@@ -4,4 +4,19 @@ import "github.com/zeromicro/go-zero/rest"
type Config struct {
rest.RestConf
Database DatabaseConfig
S3 S3Config
}
type DatabaseConfig struct {
DSN string
}
type S3Config struct {
Region string
Bucket string
AccessKeyID string
SecretAccessKey string
Endpoint string `json:",optional"` // Optional: for S3-compatible services
PresignedURLExpiration int64 `json:",default=3600"` // Default 1 hour
}

View File

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

View File

@@ -25,7 +25,7 @@ func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
}
func (l *PingLogic) Ping(req *types.PingReq) (resp *types.PingResp, err error) {
// todo: add your logic here and delete this line
return
return &types.PingResp{
Ok: true,
}, nil
}

View File

@@ -2,10 +2,15 @@ package logic
import (
"context"
"fmt"
"path/filepath"
"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/google/uuid"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -25,7 +30,42 @@ func NewUploadPresignedURLLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *UploadPresignedURLLogic) UploadPresignedURL(req *types.UploadPresignedURLReq) (resp *types.UploadPresignedURLResp, err error) {
// todo: add your logic here and delete this line
// Generate unique file key
ext := filepath.Ext(req.File_name)
fileKey := fmt.Sprintf("%s%s", uuid.New().String(), ext)
return
// Calculate expiration time
expiration := time.Duration(l.svcCtx.Config.S3.PresignedURLExpiration) * time.Second
expireAt := time.Now().Add(expiration)
// Create presigned client
presignClient := s3.NewPresignClient(l.svcCtx.S3Client)
// Generate presigned PUT URL
putObjectInput := &s3.PutObjectInput{
Bucket: &l.svcCtx.Config.S3.Bucket,
Key: &fileKey,
}
presignedReq, err := presignClient.PresignPutObject(l.ctx, putObjectInput, func(opts *s3.PresignOptions) {
opts.Expires = expiration
})
if err != nil {
l.Errorf("Failed to generate presigned URL: %v", err)
return nil, err
}
// Insert record into tmpfiles table
query := `INSERT INTO tmpfiles (file_name, key, presigned_url, expiration) VALUES ($1, $2, $3, $4)`
_, err = l.svcCtx.DB.ExecContext(l.ctx, query, req.File_name, fileKey, presignedReq.URL, expireAt)
if err != nil {
l.Errorf("Failed to insert tmpfile record: %v", err)
return nil, err
}
return &types.UploadPresignedURLResp{
Url: presignedReq.URL,
File_key: fileKey,
Expire_at: expireAt.Unix(),
}, nil
}

View File

@@ -1,15 +1,80 @@
package svc
import (
"context"
"database/sql"
"git.cialloo.com/CiallooWeb/Blog/app/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"
_ "github.com/lib/pq"
)
type ServiceContext struct {
Config config.Config
Config config.Config
S3Client *s3.Client
DB *sql.DB
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Config: c,
S3Client: initS3Client(c.S3),
DB: initDatabase(c.Database),
}
}
func initDatabase(dbConfig config.DatabaseConfig) *sql.DB {
db, err := sql.Open("postgres", dbConfig.DSN)
if err != nil {
panic(err)
}
// Verify connection
if err := db.Ping(); err != nil {
panic(err)
}
return db
}
func initS3Client(s3Config config.S3Config) *s3.Client {
var cfg aws.Config
var err error
if s3Config.Endpoint != "" {
// Custom endpoint (e.g., MinIO)
cfg, err = awsconfig.LoadDefaultConfig(context.Background(),
awsconfig.WithRegion(s3Config.Region),
awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
s3Config.AccessKeyID,
s3Config.SecretAccessKey,
"",
)),
)
if err != nil {
panic(err)
}
return s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(s3Config.Endpoint)
o.UsePathStyle = true
})
}
// Standard AWS S3
cfg, err = awsconfig.LoadDefaultConfig(context.Background(),
awsconfig.WithRegion(s3Config.Region),
awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
s3Config.AccessKeyID,
s3Config.SecretAccessKey,
"",
)),
)
if err != nil {
panic(err)
}
return s3.NewFromConfig(cfg)
}