@@ -24,34 +24,30 @@ import (
2424 "log"
2525 "math/rand"
2626 "net/url"
27- "os"
28- "strings"
2927 "time"
3028
3129 "github.com/minio/minio-go/v7"
3230 "github.com/minio/minio-go/v7/pkg/credentials"
3331
34- "github.com/coze-dev/coze-studio/backend/infra/contract/imagex"
3532 "github.com/coze-dev/coze-studio/backend/infra/contract/storage"
3633 "github.com/coze-dev/coze-studio/backend/infra/impl/storage/proxy"
37- "github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
38- "github.com/coze-dev/coze-studio/backend/types/consts"
34+ "github.com/coze-dev/coze-studio/backend/pkg/logs"
3935)
4036
4137type minioClient struct {
42- host string
4338 client * minio.Client
4439 accessKeyID string
4540 secretAccessKey string
4641 bucketName string
4742 endpoint string
4843}
4944
50- func NewStorageImagex (ctx context.Context , endpoint , accessKeyID , secretAccessKey , bucketName string , useSSL bool ) (imagex. ImageX , error ) {
45+ func New (ctx context.Context , endpoint , accessKeyID , secretAccessKey , bucketName string , useSSL bool ) (storage. Storage , error ) {
5146 m , err := getMinioClient (ctx , endpoint , accessKeyID , secretAccessKey , bucketName , useSSL )
5247 if err != nil {
5348 return nil , err
5449 }
50+
5551 return m , nil
5652}
5753
@@ -76,14 +72,8 @@ func getMinioClient(_ context.Context, endpoint, accessKeyID, secretAccessKey, b
7672 if err != nil {
7773 return nil , fmt .Errorf ("init minio client failed %v" , err )
7874 }
79- return m , nil
80- }
8175
82- func New (ctx context.Context , endpoint , accessKeyID , secretAccessKey , bucketName string , useSSL bool ) (storage.Storage , error ) {
83- m , err := getMinioClient (ctx , endpoint , accessKeyID , secretAccessKey , bucketName , useSSL )
84- if err != nil {
85- return nil , err
86- }
76+ // m.test()
8777 return m , nil
8878}
8979
@@ -109,6 +99,8 @@ func (m *minioClient) test() {
10999 ctx := context .Background ()
110100 objectName := fmt .Sprintf ("test-file-%d.txt" , rand .Int ())
111101
102+ m .ListObjects (ctx , "" )
103+
112104 err := m .PutObject (ctx , objectName , []byte ("hello content" ), storage .WithContentType ("text/plain" ))
113105 if err != nil {
114106 log .Fatalf ("upload file failed: %v" , err )
@@ -223,47 +215,48 @@ func (m *minioClient) GetObjectUrl(ctx context.Context, objectKey string, opts .
223215 return presignedURL .String (), nil
224216}
225217
226- func (m * minioClient ) GetUploadHost (ctx context.Context ) string {
227- currentHost , ok := ctxcache .Get [string ](ctx , consts .HostKeyInCtx )
228- if ! ok {
229- return ""
218+ func (m * minioClient ) ListObjectsPaginated (ctx context.Context , input * storage.ListObjectsPaginatedInput ) (* storage.ListObjectsPaginatedOutput , error ) {
219+ if input == nil {
220+ return nil , fmt .Errorf ("input cannot be nil" )
230221 }
231- return currentHost + consts .ApplyUploadActionURI
232- }
233-
234- func (m * minioClient ) GetServerID () string {
235- return ""
236- }
237-
238- func (m * minioClient ) GetUploadAuth (ctx context.Context , opt ... imagex.UploadAuthOpt ) (* imagex.SecurityToken , error ) {
239- scheme := strings .ToLower (os .Getenv (consts .StorageUploadHTTPScheme ))
240- if scheme == "" {
241- scheme = "http"
222+ if input .PageSize <= 0 {
223+ return nil , fmt .Errorf ("page size must be positive" )
242224 }
243- return & imagex.SecurityToken {
244- AccessKeyID : "" ,
245- SecretAccessKey : "" ,
246- SessionToken : "" ,
247- ExpiredTime : time .Now ().Add (time .Hour ).Format ("2006-01-02 15:04:05" ),
248- CurrentTime : time .Now ().Format ("2006-01-02 15:04:05" ),
249- HostScheme : scheme ,
250- }, nil
251- }
252225
253- func (m * minioClient ) GetResourceURL (ctx context.Context , uri string , opts ... imagex.GetResourceOpt ) (* imagex.ResourceURL , error ) {
254- url , err := m .GetObjectUrl (ctx , uri )
226+ files , err := m .ListObjects (ctx , input .Prefix )
255227 if err != nil {
256228 return nil , err
257229 }
258- return & imagex.ResourceURL {
259- URL : url ,
230+
231+ return & storage.ListObjectsPaginatedOutput {
232+ Files : files ,
233+ IsTruncated : false ,
234+ Cursor : "" ,
260235 }, nil
261236}
262237
263- func (m * minioClient ) Upload (ctx context.Context , data []byte , opts ... imagex.UploadAuthOpt ) (* imagex.UploadResult , error ) {
264- return nil , nil
265- }
238+ func (m * minioClient ) ListObjects (ctx context.Context , prefix string ) ([]* storage.FileInfo , error ) {
239+ opts := minio.ListObjectsOptions {
240+ Prefix : prefix ,
241+ Recursive : true ,
242+ }
243+
244+ objectCh := m .client .ListObjects (ctx , m .bucketName , opts )
245+
246+ var files []* storage.FileInfo
247+ for object := range objectCh {
248+ if object .Err != nil {
249+ return nil , object .Err
250+ }
251+ files = append (files , & storage.FileInfo {
252+ Key : object .Key ,
253+ LastModified : object .LastModified ,
254+ ETag : object .ETag ,
255+ Size : object .Size ,
256+ })
257+
258+ logs .CtxDebugf (ctx , "key = %s, lastModified = %s, eTag = %s, size = %d" , object .Key , object .LastModified , object .ETag , object .Size )
259+ }
266260
267- func (m * minioClient ) GetUploadAuthWithExpire (ctx context.Context , expire time.Duration , opt ... imagex.UploadAuthOpt ) (* imagex.SecurityToken , error ) {
268- return nil , nil
261+ return files , nil
269262}
0 commit comments