1 Star 1 Fork 0

linngc / center.gf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
minio_bucket.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2023-07-29 15:04 . build:编译构建
// Package minio
// @Link https://gitee.com/linngc/center.gf
// @Copyright Copyright (c) 2022 center CLI
// @Author linngc
// @License
package minio
import (
"bytes"
"context"
"gitee.com/linngc/center.gf/contrib/module/cachebuffer/internal/consts"
"gitee.com/linngc/center.gf/contrib/module/cachebuffer/model/marshal"
"github.com/gogf/gf/v2/crypto/gmd5"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/s3utils"
"io"
"path"
)
func (c *Minio) BucketInfo(ctx context.Context) (*marshal.BucketInfo, error) {
var useTotal int64
for objectInfo := range c.minioClient.ListObjects(ctx, c.Bucket, c.listObjectsOptions()) {
if objectInfo.Err != nil {
return nil, objectInfo.Err
} else {
useTotal = useTotal + objectInfo.Size
}
}
return &marshal.BucketInfo{Bucket: c.Bucket, Use: useTotal}, nil
}
// GetLocation 组装并获取存储位置
func (c *Minio) GetLocation(moduleName string) string {
return path.Join(c.Bucket, c.position, moduleName)
}
// GetData 组装数据存储对象
func (c *Minio) GetData(moduleName string, data []byte) (m *marshal.ObjectsInfo) {
m = &marshal.ObjectsInfo{Bucket: c.Bucket, Cache: c.CosType, Name: moduleName, Location: c.GetLocation(moduleName), Size: int64(len(data)), Bytes: data}
return
}
// readerObject 获取对象
// @param moduleNames 参数是 文件模块名称(请求的文件全路径名称)
func (c *Minio) readerObject(ctx context.Context, moduleName string) (reader io.Reader, err error) {
if nil == c.minioClient {
return nil, gerror.NewCode(gcode.CodeMissingConfiguration, "初始化minio异常")
}
object, err := c.minioClient.GetObject(ctx, c.Bucket, c.GetLocation(moduleName), minio.GetObjectOptions{})
defer object.Close()
if err == nil {
return object, nil
}
return nil, err
}
// fromObject 上传对象
// @param minioClient minio链接对象
// @param moduleNames 参数是 文件模块名称(请求的文件全路径名称)
// @param reader 文件byte信息
func (c *Minio) fromObject(ctx context.Context, moduleName string, reader []byte) (err error) {
if nil == c.minioClient {
return gerror.NewCode(gcode.CodeMissingConfiguration, "初始化minio异常")
}
if len(reader) == 0 {
return gerror.NewCode(gcode.CodeMissingConfiguration, "reader data is null")
}
// Input validation.
if err := s3utils.CheckValidBucketName(c.Bucket); err != nil {
return err
}
if err := s3utils.CheckValidObjectName(c.GetLocation(moduleName)); err != nil {
return err
}
_, err = c.minioClient.PutObject(ctx, c.Bucket, c.GetLocation(moduleName), bytes.NewReader(reader), int64(len(reader)), minio.PutObjectOptions{ContentType: consts.ContentTypeBinary})
return
}
func (c *Minio) makeBucketOptions() minio.MakeBucketOptions {
opts := minio.MakeBucketOptions{}
region := c.Region
if len(region) != 0 {
opts = minio.MakeBucketOptions{Region: region}
}
return opts
}
func (c *Minio) listObjectsOptions() minio.ListObjectsOptions {
return minio.ListObjectsOptions{UseV1: true, Prefix: "×", Recursive: true}
}
func (c *Minio) respBody(reader io.Reader) ([]byte, string) {
data, _ := io.ReadAll(reader)
md5, _ := gmd5.EncryptBytes(data)
return data, md5
}
Go
1
https://gitee.com/linngc/center.gf.git
git@gitee.com:linngc/center.gf.git
linngc
center.gf
center.gf
52e4a05782b6

搜索帮助