package oss import ( "errors" "eta_gn/eta_obs/global" "eta_gn/eta_obs/services/alarm_msg" "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "time" "eta_gn/eta_obs/utils" "github.com/aliyun/alibaba-cloud-sdk-go/services/sts" ) type STSToken struct { AccessKeyId string AccessKeySecret string SecurityToken string ExpiredTime string RegionId string Bucketname string Endpoint string Imghost string } // GetOssSTSToken 获取STSToken func GetOssSTSToken() (item *STSToken, err error) { defer func() { if err != nil { global.FILE_LOG.Info(err.Error()) go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3) } }() item = new(STSToken) // 已过期则获取新的token newToken, e := NewSTSToken() if e != nil { err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error()) return } item = newToken return } // NewSTSToken 获取一个新的STSToken func NewSTSToken() (item *STSToken, err error) { defer func() { if err != nil { global.FILE_LOG.Info("获取阿里云NewSTSToken失败", err.Error()) } }() item = new(STSToken) client, e := sts.NewClientWithAccessKey(global.CONFIG.Oss.RegionId, global.CONFIG.Oss.Ram.AccessKeyId, global.CONFIG.Oss.Ram.AccessKeySecret) if e != nil { err = errors.New("NewSTSToken NewClient Err: " + e.Error()) return } request := sts.CreateAssumeRoleRequest() request.Scheme = global.CONFIG.Oss.Ram.StsScheme request.RegionId = global.CONFIG.Oss.RegionId request.RoleArn = global.CONFIG.Oss.Ram.RoleArn now := time.Now().Format(utils.FormatDateTimeUnSpace) request.RoleSessionName = global.CONFIG.Oss.Ram.RoleSessionName + now request.DurationSeconds = "3600" request.ConnectTimeout = 300 * time.Second request.ReadTimeout = 300 * time.Second 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) item.RegionId = global.CONFIG.Oss.RegionId item.Bucketname = global.CONFIG.Oss.BucketName item.Endpoint = global.CONFIG.Oss.EndPoint item.Imghost = global.CONFIG.Oss.ImgHost } return } // UploadAliyunToDir 上传至hzchart //func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) { // if utils.AccessKeyId == `` { // return "0", errors.New("阿里云信息未配置") // } // 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 // } // if uploadDir == "" { // uploadDir = utils.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 = utils.Imghost + path // return path, err //} type AliOss struct{} // UploadFile 上传文件 func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) { if global.CONFIG.Oss.AccessKeyId == `` { return "0", errors.New("阿里云信息未配置") } client, err := oss.New(global.CONFIG.Oss.EndPoint, global.CONFIG.Oss.AccessKeyId, global.CONFIG.Oss.AccessKeySecret) if err != nil { return "1", err } bucket, err := client.Bucket(global.CONFIG.Oss.BucketName) if err != nil { return "2", err } path := savePath if savePath == "" { path = global.CONFIG.Oss.UploadDir + time.Now().Format("200601/20060102/") + fileName } err = bucket.PutObjectFromFile(path, filePath) if err != nil { return "3", err } resourceUrl := global.CONFIG.Oss.ImgHost + path return resourceUrl, err } func (m *AliOss) GetUploadToken() (token OssToken, err error) { stsToken, e := GetOssSTSToken() if e != nil { err = fmt.Errorf("GetOssSTSToken err: %s", e.Error()) return } token.AccessKeyId = stsToken.AccessKeyId token.AccessKeySecret = stsToken.AccessKeySecret token.SecurityToken = stsToken.SecurityToken token.ExpiredTime = stsToken.ExpiredTime token.RegionId = stsToken.RegionId token.Bucketname = stsToken.Bucketname token.Endpoint = stsToken.Endpoint token.Imghost = stsToken.Imghost return }