123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- package services
- import (
- "encoding/json"
- "errors"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "hongze/hz_crm_api/services/alarm_msg"
- "os"
- "time"
- "github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
- "hongze/hz_crm_api/utils"
- )
- var (
- RoleArn = "acs:ram::1884217364581072:role/hzossrole"
- RoleSessionName = "hzossRole"
- RAMAccessKeyId = "LTAI5t9S36LXduhTnECVY6Hn"
- RAMAccessKeySecret = "V6FG5bdzjKKqMpqEqxeWijJCdzDCuL"
- STSTokenCacheKey = "hongze_admin_sts_token"
- )
- /*
- 上传demo
- func init() {
- fmt.Println("start")
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ".jpg"
- fmt.Println("fileName:",fileName)
- fpath:="./1.png"
- resourceUrl,err:=UploadAliyun(fileName,fpath)
- if err!=nil {
- fmt.Println("UploadAliyun Err:",err.Error())
- return
- }
- fmt.Println("resourceUrl:",resourceUrl)
- fmt.Println("end")
- }
- */
- // 图片上传到阿里云
- func UploadAliyun(filename, filepath string) (string, error) {
- client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
- if err != nil {
- return "1", err
- }
- bucket, err := client.Bucket(utils.Bucketname)
- if err != nil {
- return "2", err
- }
- path := utils.Upload_dir + time.Now().Format("200601/20060102/")
- path += filename
- err = bucket.PutObjectFromFile(path, filepath)
- if err != nil {
- return "3", err
- }
- path = utils.Imghost + path
- return path, err
- }
- // UploadAliyunV2 图片上传到阿里云
- func UploadAliyunV2(filename, filepath string) (string, error) {
- client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
- if err != nil {
- return "1", err
- }
- bucket, err := client.Bucket(Bucketname)
- if err != nil {
- return "2", err
- }
- path := UploadDir + time.Now().Format("200601/20060102/")
- path += filename
- err = bucket.PutObjectFromFile(path, filepath)
- if err != nil {
- return "3", err
- }
- path = Imghost + path
- return path, err
- }
- // 音频上传到阿里云
- func UploadAudioAliyun(filename, filepath string) (string, error) {
- client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
- if err != nil {
- return "1", err
- }
- bucket, err := client.Bucket(utils.Bucketname)
- if err != nil {
- return "2", err
- }
- path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
- path += filename
- err = bucket.PutObjectFromFile(path, filepath)
- if err != nil {
- return "3", err
- }
- path = utils.Imghost + path
- return path, err
- }
- // 视频上传到阿里云
- func UploadVideoAliyun(filename, filepath, savePath string) error {
- defer func() {
- os.Remove(filepath)
- }()
- client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
- if err != nil {
- return err
- }
- bucket, err := client.Bucket(utils.Bucketname)
- if err != nil {
- return err
- }
- //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
- //path += filename
- err = bucket.PutObjectFromFile(savePath, filepath)
- if err != nil {
- return err
- }
- //path = utils.Imghost + path
- //return path,err
- return err
- }
- const (
- HzEndpoint = "oss-cn-shanghai.aliyuncs.com"
- HzBucketName string = "hzchart"
- )
- // 上传文件到阿里云
- func UploadFileToAliyun(filename, filepath, savePath string) error {
- defer func() {
- os.Remove(filepath)
- }()
- client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
- if err != nil {
- return err
- }
- bucket, err := client.Bucket(HzBucketName)
- if err != nil {
- return err
- }
- //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
- //path += filename
- err = bucket.PutObjectFromFile(savePath, filepath)
- if err != nil {
- return err
- }
- //path = utils.Imghost + path
- //return path,err
- return err
- }
- type STSToken struct {
- AccessKeyId string
- AccessKeySecret string
- SecurityToken string
- ExpiredTime string
- }
- // GetOssSTSToken 获取STSToken
- func GetOssSTSToken() (item *STSToken, err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Info(err.Error())
- go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
- }
- }()
- item = new(STSToken)
- // 获取缓存中的Token
- recent, _ := utils.Rc.RedisString(STSTokenCacheKey)
- if recent != "" {
- lastToken := new(STSToken)
- if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
- err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
- return
- }
- // 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
- afterTime := time.Now().Local().Add(10 * time.Minute)
- expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
- if e != nil {
- err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
- return
- }
- if expired.After(afterTime) {
- item.AccessKeyId = lastToken.AccessKeyId
- item.AccessKeySecret = lastToken.AccessKeySecret
- item.SecurityToken = lastToken.SecurityToken
- item.ExpiredTime = lastToken.ExpiredTime
- return
- }
- }
- // 已过期则获取新的token
- newToken, e := NewSTSToken()
- if e != nil {
- err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
- return
- }
- newTokenJson, e := json.Marshal(newToken)
- if e != nil {
- err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
- return
- }
- // 覆盖缓存
- if e := utils.Rc.Put(STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
- err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
- return
- }
- item = newToken
- return
- }
- // NewSTSToken 获取一个新的STSToken
- func NewSTSToken() (item *STSToken, err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Info(err.Error())
- }
- }()
- item = new(STSToken)
- client, e := sts.NewClientWithAccessKey("cn-shanghai", RAMAccessKeyId, RAMAccessKeySecret)
- if e != nil {
- err = errors.New("NewSTSToken NewClient Err: " + e.Error())
- return
- }
- request := sts.CreateAssumeRoleRequest()
- request.Scheme = "https"
- request.RegionId = "cn-shanghai"
- request.RoleArn = RoleArn
- now := time.Now().Format(utils.FormatDateTimeUnSpace)
- request.RoleSessionName = RoleSessionName + now
- request.DurationSeconds = "3600"
- response, e := client.AssumeRole(request)
- if e != nil {
- err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
- return
- }
- if response != nil {
- item.AccessKeyId = response.Credentials.AccessKeyId
- item.AccessKeySecret = response.Credentials.AccessKeySecret
- item.SecurityToken = response.Credentials.SecurityToken
- t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
- expiration := t.In(time.Local)
- item.ExpiredTime = expiration.Format(utils.FormatDateTime)
- }
- return
- }
- // 后台用于传研报小程序二维码
- var (
- Bucketname string = "hzchart"
- Endpoint string = "oss-cn-shanghai.aliyuncs.com"
- Imghost string = "https://hzstatic.hzinsights.com/"
- UploadDir string = "static/images/"
- AccessKeyId string = "LTAIFMZYQhS2BTvW"
- AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
- )
- // UploadAliyunToDir 上传至hzchart
- func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) {
- client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
- if err != nil {
- return "1", err
- }
- bucket, err := client.Bucket(Bucketname)
- if err != nil {
- return "2", err
- }
- if uploadDir == "" {
- uploadDir = UploadDir
- }
- if fileDir == "" {
- fileDir = time.Now().Format("200601/20060102/")
- }
- path := uploadDir + fileDir
- path += filename
- err = bucket.PutObjectFromFile(path, filepath)
- if err != nil {
- return "3", err
- }
- path = Imghost + path
- return path, err
- }
|