package services import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" "hongze/hongze_yb/global" "os" "time" ) /* 上传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") } */ // UploadAliyun 图片上传到阿里云 func UploadAliyun(filename, filepath string) (string, error) { client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret) if err != nil { return "1", err } bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName) if err != nil { return "2", err } path := global.CONFIG.AliOss.UploadDir + time.Now().Format("200601/20060102/") path += filename err = bucket.PutObjectFromFile(path, filepath) if err != nil { return "3", err } path = global.CONFIG.AliOss.ImgHost + path return path, err } // UploadAudioAliyun 音频上传到阿里云 func UploadAudioAliyun(filename, filepath string) (string, error) { client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret) if err != nil { return "1", err } bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName) if err != nil { return "2", err } path := global.CONFIG.AliOss.UploadAudioDir + time.Now().Format("200601/20060102/") path += filename err = bucket.PutObjectFromFile(path, filepath) if err != nil { return "3", err } path = global.CONFIG.AliOss.ImgHost + path return path, err } // UploadVideoAliyun 视频上传到阿里云 func UploadVideoAliyun(filename, filepath, savePath string) error { defer func() { os.Remove(filepath) }() client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret) if err != nil { return err } bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName) if err != nil { return err } //path := global.CONFIG.AliOss.UploadAudioDir + time.Now().Format("200601/20060102/") //path += filename err = bucket.PutObjectFromFile(savePath, filepath) if err != nil { return err } //path = global.CONFIG.AliOss.ImgHost + path //return path,err return err } var ( Bucketname string = "hzchart" Endpoint string = "oss-cn-shanghai.aliyuncs.com" ResourceHost string = "https://hzstatic.hzinsights.com/" StaticDir string = "static/" AccessKeyId string = "LTAIFMZYQhS2BTvW" AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq" ) // UploadAliyunToDir func UploadAliyunToDir(filename, filepath, 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 fileDir == "" { fileDir = time.Now().Format("200601/20060102/") } path := StaticDir + fileDir path += filename err = bucket.PutObjectFromFile(path, filepath) if err != nil { return "3", err } path = ResourceHost + path return path, err }