63 lines
977 B
Go
63 lines
977 B
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
const (
|
|
minioEndpoint = "ai.ronsunny.cn:9000"
|
|
minioAccessKey = "minioadmin"
|
|
minioSecretKey = "minioadmin"
|
|
useSSL = true
|
|
)
|
|
|
|
var client *minio.Client
|
|
|
|
func Init() error {
|
|
if client != nil {
|
|
return nil
|
|
}
|
|
|
|
c, err := minio.New(minioEndpoint, &minio.Options{
|
|
Creds: credentials.NewStaticV4(minioAccessKey, minioSecretKey, ""),
|
|
Secure: useSSL,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client = c
|
|
return nil
|
|
}
|
|
func UploadFile(
|
|
ctx context.Context,
|
|
bucketName string,
|
|
objectName string,
|
|
filePath string,
|
|
contentType string,
|
|
) (int64, error) {
|
|
|
|
if client == nil {
|
|
return 0, errors.New("minio client not initialized")
|
|
}
|
|
|
|
info, err := client.FPutObject(
|
|
ctx,
|
|
bucketName,
|
|
objectName,
|
|
filePath,
|
|
minio.PutObjectOptions{
|
|
ContentType: contentType,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return info.Size, nil
|
|
}
|