Files
workspace/code/allapp/allapp-go-v3/pkg/s3store/index.go
T
2026-04-19 21:30:51 +08:00

53 lines
1.2 KiB
Go

package s3store
import (
"allapp-go/internal/config"
"context"
"fmt"
"mime/multipart"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var S3Client *s3.Client
var presignClient *s3.PresignClient
var bucket string
func NewS3Client(cfg *config.Config) {
credProvider := credentials.NewStaticCredentialsProvider(
cfg.AwsS3.AccessKey,
cfg.AwsS3.SecretKey,
"",
)
awsCfg := aws.Config{
Region: cfg.AwsS3.Region,
Credentials: credProvider,
}
S3Client = s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(cfg.AwsS3.Endpoint)
o.UsePathStyle = true
})
presignClient = s3.NewPresignClient(S3Client)
bucket = cfg.AwsS3.Bucket
fmt.Println("S3 client and Presign client initialized successfully!")
}
func UploadToRustFS(ctx context.Context, key string, file multipart.File, size int64, contentType string) error {
_, err := S3Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
ContentLength: aws.Int64(size),
ContentType: aws.String(contentType),
})
return err
}