Skip to content

Commit 826e116

Browse files
committed
feat(infra): support uploading files via io.Reader
1 parent 6fa2acf commit 826e116

7 files changed

Lines changed: 33 additions & 319 deletions

File tree

backend/domain/knowledge/service/retrieve_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424
"testing"
2525

2626
"github.com/cloudwego/eino/schema"
27+
"github.com/stretchr/testify/assert"
28+
"go.uber.org/mock/gomock"
29+
"gorm.io/driver/mysql"
30+
"gorm.io/gorm"
31+
2732
"github.com/coze-dev/coze-studio/backend/domain/knowledge/entity"
2833
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
2934
"github.com/coze-dev/coze-studio/backend/domain/knowledge/repository"
@@ -35,10 +40,6 @@ import (
3540
mock_db "github.com/coze-dev/coze-studio/backend/internal/mock/infra/contract/rdb"
3641
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
3742
"github.com/coze-dev/coze-studio/backend/pkg/lang/sets"
38-
"github.com/stretchr/testify/assert"
39-
"go.uber.org/mock/gomock"
40-
"gorm.io/driver/mysql"
41-
"gorm.io/gorm"
4243
)
4344

4445
func TestAddSliceIdColumn(t *testing.T) {

backend/infra/contract/storage/option.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type PutOption struct {
3838
ContentDisposition *string
3939
ContentLanguage *string
4040
Expires *time.Time
41+
ObjectSize int64
4142
}
4243

4344
type PutOptFn func(option *PutOption)
@@ -48,6 +49,12 @@ func WithContentType(v string) PutOptFn {
4849
}
4950
}
5051

52+
func WithObjectSize(v int64) PutOptFn {
53+
return func(o *PutOption) {
54+
o.ObjectSize = v
55+
}
56+
}
57+
5158
func WithContentEncoding(v string) PutOptFn {
5259
return func(o *PutOption) {
5360
o.ContentEncoding = &v

backend/infra/contract/storage/storage.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package storage
1818

19-
import "context"
19+
import (
20+
"context"
21+
"io"
22+
)
2023

2124
//go:generate mockgen -destination ../../../internal/mock/infra/contract/storage/storage_mock.go -package mock -source storage.go Factory
2225
type Storage interface {
2326
PutObject(ctx context.Context, objectKey string, content []byte, opts ...PutOptFn) error
27+
PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...PutOptFn) error
2428
GetObject(ctx context.Context, objectKey string) ([]byte, error)
2529
DeleteObject(ctx context.Context, objectKey string) error
2630
GetObjectUrl(ctx context.Context, objectKey string, opts ...GetOptFn) (string, error)

backend/infra/impl/storage/minio/minio.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ func (m *minioClient) test() {
138138
}
139139

140140
func (m *minioClient) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
141+
opts = append(opts, storage.WithObjectSize(int64(len(content))))
142+
return m.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
143+
}
144+
145+
func (m *minioClient) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
141146
option := storage.PutOption{}
142147
for _, opt := range opts {
143148
opt(&option)
@@ -165,7 +170,7 @@ func (m *minioClient) PutObject(ctx context.Context, objectKey string, content [
165170
}
166171

167172
_, err := m.client.PutObject(ctx, m.bucketName, objectKey,
168-
bytes.NewReader(content), int64(len(content)), minioOpts)
173+
content, option.ObjectSize, minioOpts)
169174
if err != nil {
170175
return fmt.Errorf("PutObject failed: %v", err)
171176
}

backend/infra/impl/storage/s3/s3.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,18 @@ func (t *s3Client) CheckAndCreateBucket(ctx context.Context) error {
152152
}
153153

154154
func (t *s3Client) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
155+
return t.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
156+
}
157+
158+
func (t *s3Client) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
155159
client := t.client
156-
body := bytes.NewReader(content)
157160
bucket := t.bucketName
158161

159162
// upload object
160163
_, err := client.PutObject(ctx, &s3.PutObjectInput{
161164
Bucket: aws.String(bucket),
162165
Key: aws.String(objectKey),
163-
Body: body,
166+
Body: content,
164167
})
165168
return err
166169
}

backend/infra/impl/storage/tos/tos.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,20 @@ func (t *tosClient) CheckAndCreateBucket(ctx context.Context) error {
140140

141141
return err
142142
}
143-
144143
func (t *tosClient) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
144+
return t.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
145+
}
146+
147+
func (t *tosClient) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
145148
client := t.client
146-
body := bytes.NewReader(content)
147149
bucketName := t.bucketName
148150

149151
_, err := client.PutObjectV2(ctx, &tos.PutObjectV2Input{
150152
PutObjectBasicInput: tos.PutObjectBasicInput{
151153
Bucket: bucketName,
152154
Key: objectKey,
153155
},
154-
Content: body,
156+
Content: content,
155157
})
156158

157159
// logs.CtxDebugf(ctx, "PutObject resp: %v, err: %v", conv.DebugJsonToStr(output), err)

0 commit comments

Comments
 (0)