t提交
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package ossx
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"go_mysqldump/global"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
)
|
||||
|
||||
// 上传文件
|
||||
func Upload(objectKey string, filePath string) error {
|
||||
// 替换
|
||||
objectKey = strings.ReplaceAll(objectKey, `\`, `/`)
|
||||
// fmt.Println("objectKey:", objectKey, " filePath:", filePath)
|
||||
|
||||
client, err := oss.New(global.Config.Oss.Endpoint, global.Config.Oss.AccessKeyID, global.Config.Oss.AccessKeySecret)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket, err := client.Bucket(global.Config.Oss.BucketName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = bucket.PutObjectFromFile(objectKey, filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
props, err := bucket.GetObjectDetailedMeta(objectKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
etag := props.Values("Etag")
|
||||
if len(etag) == 0 {
|
||||
return errors.New("获取远端md5错误")
|
||||
}
|
||||
remoteMd5 := strings.Trim(etag[0], `"`)
|
||||
|
||||
// fmt.Println("remoteMd5", remoteMd5)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
io.Copy(hash, file)
|
||||
localMd5 := strings.ToUpper(hex.EncodeToString(hash.Sum(nil)))
|
||||
|
||||
if remoteMd5 != localMd5 {
|
||||
return errors.New("md5")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user