123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package services
- import (
- "errors"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "os"
- "time"
- "eta_gn/eta_report/utils"
- )
- // 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) {
- defer func() {
- _ = os.Remove(filePath)
- }()
- 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
- }
- path := savePath
- if savePath == "" {
- path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
- }
- err = bucket.PutObjectFromFile(path, filePath)
- if err != nil {
- return "3", err
- }
- resourceUrl := utils.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
- }
|