1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package services
- import (
- "errors"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "os"
- "time"
- "eta/eta_task/utils"
- )
- /*
- 上传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.UploadDir + 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
- }
- // UploadAudioAliyun 音频上传到阿里云
- func UploadAudioAliyun(filename, filepath 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
- }
- 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
- }
|