123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package services
- import (
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "os"
- "time"
- "hongze/hongze_mfyx_gzh/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.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
- }
- //音频上传到阿里云
- 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
- }
- //PDF上传到阿里云
- func UploadPdfAliyun(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_Pdf_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
- }
- 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
- }
|