feat(obs): 支持华为云obs存储
This commit is contained in:
@@ -102,6 +102,25 @@ func NewFileServiceFromStorageConfig(
|
||||
svc, err := NewS3FileService(sec.S3.Endpoint, sec.S3.AccessKey, sec.S3.SecretKey, sec.S3.BucketName, sec.S3.Region, pathPrefix)
|
||||
return svc, p, err
|
||||
|
||||
case "obs":
|
||||
obsEndpoint := strings.TrimSpace(os.Getenv("OBS_ENDPOINT"))
|
||||
obsRegion := strings.TrimSpace(os.Getenv("OBS_REGION"))
|
||||
obsAccessKey := strings.TrimSpace(os.Getenv("OBS_ACCESS_KEY"))
|
||||
obsSecretKey := strings.TrimSpace(os.Getenv("OBS_SECRET_KEY"))
|
||||
obsBucketName := strings.TrimSpace(os.Getenv("OBS_BUCKET_NAME"))
|
||||
obsPathPrefix := strings.TrimSpace(os.Getenv("OBS_PATH_PREFIX"))
|
||||
if obsPathPrefix == "" {
|
||||
obsPathPrefix = "weknora/"
|
||||
}
|
||||
if obsEndpoint == "" || obsAccessKey == "" || obsSecretKey == "" || obsBucketName == "" {
|
||||
return nil, p, fmt.Errorf("incomplete obs config")
|
||||
}
|
||||
if obsRegion == "" {
|
||||
obsRegion = "cn-north-4"
|
||||
}
|
||||
svc, err := NewObsFileService(obsEndpoint, obsRegion, obsAccessKey, obsSecretKey, obsBucketName, obsPathPrefix)
|
||||
return svc, p, err
|
||||
|
||||
case "oss":
|
||||
if sec == nil || sec.OSS == nil || sec.OSS.Endpoint == "" || sec.OSS.Region == "" || sec.OSS.AccessKey == "" || sec.OSS.SecretKey == "" || sec.OSS.BucketName == "" {
|
||||
return nil, p, fmt.Errorf("incomplete oss config")
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type obsFileService struct {
|
||||
client *s3.Client
|
||||
bucketName string
|
||||
endpoint string
|
||||
region string
|
||||
pathPrefix string
|
||||
proxyDomain string
|
||||
}
|
||||
|
||||
type obsEndpointResolver struct {
|
||||
url string
|
||||
}
|
||||
|
||||
func (r *obsEndpointResolver) ResolveEndpoint(region string, options s3.EndpointResolverOptions) (aws.Endpoint, error) {
|
||||
return aws.Endpoint{
|
||||
URL: r.url,
|
||||
SigningRegion: region,
|
||||
HostnameImmutable: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewObsFileService(
|
||||
endpoint, region, accessKeyID, secretAccessKey, bucketName string,
|
||||
pathPrefix string,
|
||||
) (interfaces.FileService, error) {
|
||||
|
||||
client := s3.New(s3.Options{
|
||||
Region: region,
|
||||
EndpointResolver: &obsEndpointResolver{url: endpoint},
|
||||
Credentials: credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, ""),
|
||||
UsePathStyle: true,
|
||||
})
|
||||
|
||||
_, err := client.HeadBucket(context.Background(), &s3.HeadBucketInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
})
|
||||
if err != nil {
|
||||
_, createErr := client.CreateBucket(context.Background(), &s3.CreateBucketInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
})
|
||||
if createErr != nil {
|
||||
fmt.Printf("Warning: bucket %s may not exist or cannot be created: %v\n", bucketName, createErr)
|
||||
}
|
||||
}
|
||||
|
||||
proxyDomain := strings.TrimSuffix(os.Getenv("OBS_PROXY_DOMAIN"), "/")
|
||||
|
||||
return &obsFileService{
|
||||
client: client,
|
||||
bucketName: bucketName,
|
||||
endpoint: endpoint,
|
||||
region: region,
|
||||
pathPrefix: strings.Trim(pathPrefix, "/"),
|
||||
proxyDomain: proxyDomain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func CheckObsConnectivity(ctx context.Context, endpoint, region, accessKey, secretKey, bucketName string) error {
|
||||
checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
client := s3.New(s3.Options{
|
||||
Region: region,
|
||||
EndpointResolver: &obsEndpointResolver{url: endpoint},
|
||||
Credentials: credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
|
||||
UsePathStyle: true,
|
||||
})
|
||||
|
||||
_, err := client.HeadBucket(checkCtx, &s3.HeadBucketInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("OBS connectivity check failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) CheckConnectivity(ctx context.Context) error {
|
||||
checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.client.HeadBucket(checkCtx, &s3.HeadBucketInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("OBS connectivity check failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) parseObsFilePath(filePath string) (string, error) {
|
||||
prefix := s.getPrifix()
|
||||
|
||||
if strings.HasPrefix(filePath, prefix) {
|
||||
rest := strings.TrimPrefix(filePath, prefix)
|
||||
// With proxy domain: path is {prefix}/{objectKey} (no bucket name)
|
||||
if s.proxyDomain != "" {
|
||||
rest = strings.TrimPrefix(rest, "/")
|
||||
if rest != "" {
|
||||
return rest, nil
|
||||
}
|
||||
return "", fmt.Errorf("invalid OBS file path: %s", filePath)
|
||||
}
|
||||
// Without proxy domain: path is {prefix}/{bucketName}/{objectKey}
|
||||
parts := strings.SplitN(rest, "/", 2)
|
||||
if len(parts) == 2 && parts[0] == s.bucketName && parts[1] != "" {
|
||||
return parts[1], nil
|
||||
}
|
||||
return "", fmt.Errorf("invalid OBS file path: %s", filePath)
|
||||
}
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) getPrifix() string {
|
||||
if s.proxyDomain != "" {
|
||||
return s.proxyDomain + "/"
|
||||
}
|
||||
return "obs://"
|
||||
}
|
||||
|
||||
func (s *obsFileService) SaveFile(ctx context.Context,
|
||||
file *multipart.FileHeader, tenantID uint64, knowledgeID string,
|
||||
) (string, error) {
|
||||
ext := filepath.Ext(file.Filename)
|
||||
|
||||
var objectKey string
|
||||
if s.pathPrefix != "" {
|
||||
objectKey = fmt.Sprintf("%s/%d/%s/%s%s", s.pathPrefix, tenantID, knowledgeID, uuid.New().String(), ext)
|
||||
} else {
|
||||
objectKey = fmt.Sprintf("%d/%s/%s%s", tenantID, knowledgeID, uuid.New().String(), ext)
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
contentType := file.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
_, err = s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
Key: aws.String(objectKey),
|
||||
Body: src,
|
||||
ContentLength: aws.Int64(file.Size),
|
||||
ContentType: aws.String(contentType),
|
||||
// ACL: "private",
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload file to OBS: %w", err)
|
||||
}
|
||||
prefix := s.getPrifix()
|
||||
if s.proxyDomain != "" {
|
||||
return fmt.Sprintf("%s%s", prefix, objectKey), nil
|
||||
}
|
||||
return fmt.Sprintf("%s%s/%s", prefix, s.bucketName, objectKey), nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
||||
objectKey, err := s.parseObsFilePath(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
output, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
Key: aws.String(objectKey),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get file from OBS: %w", err)
|
||||
}
|
||||
|
||||
return output.Body, nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) DeleteFile(ctx context.Context, filePath string) error {
|
||||
objectKey, err := s.parseObsFilePath(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
Key: aws.String(objectKey),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete file from OBS: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
objectKey, err := s.parseObsFilePath(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if s.proxyDomain != "" {
|
||||
return s.proxyDomain + "/" + strings.TrimPrefix(objectKey, "/"), nil
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s/%s", s.endpoint, s.bucketName, strings.TrimPrefix(objectKey, "/")), nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
ext := filepath.Ext(fileName)
|
||||
|
||||
var objectKey string
|
||||
if temp {
|
||||
if s.pathPrefix != "" {
|
||||
objectKey = fmt.Sprintf("%s/temp/%d/%s%s", s.pathPrefix, tenantID, uuid.New().String(), ext)
|
||||
} else {
|
||||
objectKey = fmt.Sprintf("temp/%d/%s%s", tenantID, uuid.New().String(), ext)
|
||||
}
|
||||
} else {
|
||||
if s.pathPrefix != "" {
|
||||
objectKey = fmt.Sprintf("%s/%d/%s%s", s.pathPrefix, tenantID, uuid.New().String(), ext)
|
||||
} else {
|
||||
objectKey = fmt.Sprintf("%d/%s%s", tenantID, uuid.New().String(), ext)
|
||||
}
|
||||
}
|
||||
|
||||
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
Key: aws.String(objectKey),
|
||||
Body: strings.NewReader(string(data)),
|
||||
ContentType: aws.String("application/octet-stream"),
|
||||
ACL: "public-read",
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload bytes to OBS: %w", err)
|
||||
}
|
||||
|
||||
prefix := s.getPrifix()
|
||||
if s.proxyDomain != "" {
|
||||
return fmt.Sprintf("%s%s", prefix, objectKey), nil
|
||||
}
|
||||
return fmt.Sprintf("%s%s/%s", prefix, s.bucketName, objectKey), nil
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e
|
||||
prompt = vlmOCRScannedPDFPrompt
|
||||
logger.Infof(ctx, "[ImageMultimodal] Using scanned PDF prompt for OCR: %s", payload.ImageURL)
|
||||
}
|
||||
|
||||
|
||||
ocrText, ocrErr := vlmModel.Predict(ctx, [][]byte{imgBytes}, prompt)
|
||||
if ocrErr != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] OCR failed for %s: %v", payload.ImageURL, ocrErr)
|
||||
@@ -430,7 +430,7 @@ func (s *ImageMultimodalService) checkAndFinalizeAllImages(ctx context.Context,
|
||||
}
|
||||
|
||||
redisKey := fmt.Sprintf("multimodal:pending:%s", payload.KnowledgeID)
|
||||
|
||||
|
||||
pendingCount, err := s.redisClient.Decr(ctx, redisKey).Result()
|
||||
if err != nil && err != redis.Nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] Failed to decrement pending count for %s: %v", payload.KnowledgeID, err)
|
||||
@@ -449,7 +449,7 @@ func (s *ImageMultimodalService) enqueueKnowledgePostProcessTask(ctx context.Con
|
||||
if s.taskEnqueuer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
taskPayload := types.KnowledgePostProcessPayload{
|
||||
TenantID: payload.TenantID,
|
||||
KnowledgeID: payload.KnowledgeID,
|
||||
|
||||
Reference in New Issue
Block a user