2023-12-27 19:02:23 +08:00
|
|
|
package ossx
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-31 16:36:35 +08:00
|
|
|
"bytes"
|
2023-12-27 19:02:23 +08:00
|
|
|
"errors"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
type ossx struct {
|
|
|
|
|
endPoint string
|
|
|
|
|
accessKeyId string
|
|
|
|
|
accessKeySecret string
|
|
|
|
|
bucketName string
|
|
|
|
|
client *oss.Client
|
|
|
|
|
bucket *oss.Bucket
|
|
|
|
|
}
|
2023-12-27 19:02:23 +08:00
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
func NewOssx(endPoint string, accessKeyId string, accessKeySecret string, bucketName string) (*ossx, error) {
|
|
|
|
|
client, err := oss.New(endPoint, accessKeyId, accessKeySecret)
|
2023-12-27 19:02:23 +08:00
|
|
|
if err != nil {
|
2024-01-31 16:36:35 +08:00
|
|
|
return nil, err
|
2023-12-27 19:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
bucket, err := client.Bucket(bucketName)
|
2023-12-27 19:02:23 +08:00
|
|
|
if err != nil {
|
2024-01-31 16:36:35 +08:00
|
|
|
return nil, err
|
2023-12-27 19:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
return &ossx{
|
|
|
|
|
endPoint: endPoint,
|
|
|
|
|
accessKeyId: accessKeyId,
|
|
|
|
|
accessKeySecret: accessKeySecret,
|
|
|
|
|
bucketName: bucketName,
|
|
|
|
|
client: client,
|
|
|
|
|
bucket: bucket,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上传文件
|
|
|
|
|
func (l *ossx) UploadByPath(objectKey string, filePath string) error {
|
|
|
|
|
// 替换
|
|
|
|
|
objectKey = strings.ReplaceAll(objectKey, `\`, `/`)
|
|
|
|
|
|
|
|
|
|
err := l.bucket.PutObjectFromFile(objectKey, filePath)
|
2023-12-27 19:02:23 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载文件
|
|
|
|
|
func (l *ossx) GetObjectToByte(objectKey string) ([]byte, error) {
|
|
|
|
|
// 替换
|
|
|
|
|
objectKey = strings.ReplaceAll(objectKey, `\`, `/`)
|
|
|
|
|
|
|
|
|
|
body, err := l.bucket.GetObject(objectKey)
|
2023-12-27 19:02:23 +08:00
|
|
|
if err != nil {
|
2024-01-31 16:36:35 +08:00
|
|
|
return nil, err
|
2023-12-27 19:02:23 +08:00
|
|
|
}
|
2024-01-31 16:36:35 +08:00
|
|
|
defer body.Close()
|
2023-12-27 19:02:23 +08:00
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
buf.ReadFrom(body)
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
|
}
|
2023-12-27 19:02:23 +08:00
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
// 分片上传
|
|
|
|
|
func (l *ossx) UploadByPathMultipart(objectKey string, filePath string) error {
|
|
|
|
|
// 替换
|
|
|
|
|
objectKey = strings.ReplaceAll(objectKey, `\`, `/`)
|
2023-12-27 19:02:23 +08:00
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
// 分片上传
|
|
|
|
|
err := l.bucket.UploadFile(objectKey, filePath, 100*1024, oss.Routines(3))
|
2023-12-27 19:02:23 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2023-12-27 19:02:23 +08:00
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
// 上传文件
|
|
|
|
|
func (l *ossx) UploadByByte(objectKey string, body []byte) error {
|
|
|
|
|
objectKey = strings.ReplaceAll(objectKey, `\`, `/`)
|
|
|
|
|
|
|
|
|
|
return l.bucket.PutObject(objectKey, strings.NewReader(string(body)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取远端MD5
|
|
|
|
|
func (l *ossx) GetObjectMd5(objectKey string) (string, error) {
|
|
|
|
|
props, err := l.bucket.GetObjectDetailedMeta(objectKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2023-12-27 19:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 16:36:35 +08:00
|
|
|
etag := props.Values("Etag")
|
|
|
|
|
if len(etag) == 0 {
|
|
|
|
|
return "", errors.New("获取远端md5错误")
|
|
|
|
|
}
|
|
|
|
|
remoteMd5 := strings.Trim(etag[0], `"`)
|
|
|
|
|
return remoteMd5, nil
|
2023-12-27 19:02:23 +08:00
|
|
|
}
|