123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- package services
- import (
- "fmt"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "os"
- "strings"
- "time"
- "hongze/hongze_cygx/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
- }
- // GetCloudDiskResourceFileTypeExtMap 常见文件类型图标
- func GetCloudDiskResourceFileTypeExtMap() map[string]string {
- return map[string]string{
- ".doc": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
- ".docx": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
- ".pdf": "https://hzstatic.hzinsights.com/static/icon/file_type_pdf.png",
- ".ppt": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
- ".pptx": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
- ".xls": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
- ".xlsx": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
- ".jpg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".jpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".png": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".bmp": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".svg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".gif": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
- ".mp4": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".wmv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".mov": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".mpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".avi": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".flv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".rm": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".rmvb": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
- ".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
- ".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
- }
- }
- //func init() {
- // DeleteUploadFileToAliyun("https://hzstatic.hzinsights.com/static/images/htm2img/9iElUd8oxpWgggf5pcKOCB8LZTiB.jpg")
- //}
- // 根据图片路径删除阿里云上的文件
- func DeleteUploadFileToAliyun(imgUrl string) error {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("err:", err)
- go utils.SendAlarmMsg("删除Oss图片失败, Err:"+err.Error()+"图片路径:"+imgUrl, 3)
- }
- }()
- client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
- if err != nil {
- return err
- }
- bucket, err := client.Bucket(HzBucketName)
- if err != nil {
- return err
- }
- var objectName string
- sliceObj := strings.Split(imgUrl, ".com/")
- if len(sliceObj) == 2 {
- objectName = sliceObj[1]
- }
- if objectName == "" {
- return err
- }
- fmt.Println(objectName)
- //return err
- // 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
- //objectName = "static/images/htm2img/G32q2Lgp6soeh7xOW12yfZ4zC2uK.jpg"
- // 删除单个文件。
- err = bucket.DeleteObject(objectName)
- if err != nil {
- return err
- }
- return err
- }
|