From c21d1edd80e1844e46d5b5e8617cacb6eef03ec5 Mon Sep 17 00:00:00 2001 From: "bingxiang.cheng" Date: Mon, 13 Apr 2026 15:24:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(oss):=20=E6=9B=B4=E6=96=B0OSS=20S3=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=A8=A1=E5=BC=8F=E7=9A=84=E6=B3=A8=E9=87=8A=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84=E6=A0=B7=E5=BC=8F=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=E5=B9=B6=E7=A6=81=E7=94=A8=E8=87=AA=E5=8A=A8=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=92=8C=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/application/service/file/oss.go | 35 ++++++++++++------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/internal/application/service/file/oss.go b/internal/application/service/file/oss.go index 4c36bb1a..fdfb9017 100644 --- a/internal/application/service/file/oss.go +++ b/internal/application/service/file/oss.go @@ -22,7 +22,7 @@ import ( ) // ossFileService implements the FileService interface for Aliyun OSS -// using the S3-compatible protocol with path-style addressing. +// using the S3-compatible protocol with virtual-hosted style addressing. type ossFileService struct { client *s3.Client tempClient *s3.Client @@ -34,11 +34,14 @@ type ossFileService struct { const ossScheme = "oss://" // newOSSClient creates a bare s3.Client configured for OSS S3-compatible mode. -// OSS always uses path-style addressing. +// OSS uses virtual-hosted style addressing and does not support aws-chunked encoding. func newOSSClient(endpoint, region, accessKey, secretKey string) (*s3.Client, error) { cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")), + // Disable automatic aws-chunked encoding — OSS does not support it. + config.WithRequestChecksumCalculation(aws.RequestChecksumCalculationWhenRequired), + config.WithResponseChecksumValidation(aws.ResponseChecksumValidationWhenRequired), ) if err != nil { return nil, fmt.Errorf("failed to load AWS config: %w", err) @@ -202,11 +205,10 @@ func (s *ossFileService) SaveFile(ctx context.Context, } _, err = s.client.PutObject(ctx, &s3.PutObjectInput{ - Bucket: aws.String(s.bucketName), - Key: aws.String(objectName), - Body: src, - ContentLength: aws.Int64(file.Size), - ContentType: aws.String(contentType), + Bucket: aws.String(s.bucketName), + Key: aws.String(objectName), + Body: src, + ContentType: aws.String(contentType), }) if err != nil { return "", fmt.Errorf("failed to upload file to OSS: %w", err) @@ -224,17 +226,15 @@ func (s *ossFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui return "", fmt.Errorf("invalid file name: %w", err) } ext := filepath.Ext(safeName) - reader := bytes.NewReader(data) // If requesting temp bucket and it is configured, use it if temp && s.tempClient != nil { objectName := fmt.Sprintf("exports/%d/%s%s", tenantID, uuid.New().String(), ext) _, err := s.tempClient.PutObject(ctx, &s3.PutObjectInput{ - Bucket: aws.String(s.tempBucketName), - Key: aws.String(objectName), - Body: reader, - ContentLength: aws.Int64(int64(len(data))), - ContentType: aws.String("text/csv; charset=utf-8"), + Bucket: aws.String(s.tempBucketName), + Key: aws.String(objectName), + Body: bytes.NewReader(data), + ContentType: aws.String("text/csv; charset=utf-8"), }) if err != nil { return "", fmt.Errorf("failed to upload bytes to OSS temp bucket: %w", err) @@ -245,11 +245,10 @@ func (s *ossFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui // Save to main bucket objectName := fmt.Sprintf("%s%d/exports/%s%s", s.pathPrefix, tenantID, uuid.New().String(), ext) _, err = s.client.PutObject(ctx, &s3.PutObjectInput{ - Bucket: aws.String(s.bucketName), - Key: aws.String(objectName), - Body: reader, - ContentLength: aws.Int64(int64(len(data))), - ContentType: aws.String("text/csv; charset=utf-8"), + Bucket: aws.String(s.bucketName), + Key: aws.String(objectName), + Body: bytes.NewReader(data), + ContentType: aws.String("text/csv; charset=utf-8"), }) if err != nil { return "", fmt.Errorf("failed to upload bytes to OSS: %w", err)