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
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package smsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type sms struct {
|
||||||
|
endPoint string
|
||||||
|
accessKeyId string
|
||||||
|
accessKeySecret string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSms(endPoint string, accessKeyId string, accessKeySecret string) *sms {
|
||||||
|
return &sms{
|
||||||
|
endPoint: endPoint,
|
||||||
|
accessKeyId: accessKeyId,
|
||||||
|
accessKeySecret: accessKeySecret,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sms) Send(phone, code, signName, templateCode string) error {
|
||||||
|
client, err := dysmsapi.NewClientWithAccessKey(s.endPoint, s.accessKeyId, s.accessKeySecret)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := dysmsapi.CreateSendSmsRequest()
|
||||||
|
req.Scheme = "https"
|
||||||
|
req.PhoneNumbers = phone
|
||||||
|
req.SignName = signName
|
||||||
|
req.TemplateCode = templateCode
|
||||||
|
req.TemplateParam = fmt.Sprintf(`{"code":"%s"}`, code)
|
||||||
|
resp, err := client.SendSms(req)
|
||||||
|
// fmt.Printf("sms send: resp:%+v err:%+v", resp, err)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp == nil {
|
||||||
|
return errors.New("没有获取到发送结果")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Code != "OK" {
|
||||||
|
return errors.New(resp.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user