1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package services
- import (
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "os"
- "time"
- "hongze/hongze_public_api/utils"
- )
- const (
- HzEndpoint = "oss-cn-shanghai.aliyuncs.com"
- HzBucketName string = "hzchart"
- )
- // 后台用于传研报小程序二维码
- var (
- Bucketname string = "hzchart"
- Endpoint string = "oss-cn-shanghai.aliyuncs.com"
- Imghost string = "https://hzstatic.hzinsights.com/"
- UploadDir string = "static/images/"
- AccessKeyId string = "LTAIFMZYQhS2BTvW"
- AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
- )
- // 上传文件到阿里云
- 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
- }
- // UploadAliYun 图片上传到阿里云
- func UploadAliYun(filename, filepath 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
- }
- path := UploadDir + time.Now().Format("200601/20060102/")
- path += filename
- err = bucket.PutObjectFromFile(path, filepath)
- if err != nil {
- return "3", err
- }
- path = Imghost + path
- return path, err
- }
|