361 lines
13 KiB
Go
361 lines
13 KiB
Go
|
|
package aliyunossservice
|
|||
|
|
|
|||
|
|
//
|
|||
|
|
//import (
|
|||
|
|
// "bytes"
|
|||
|
|
// "encoding/base64"
|
|||
|
|
// "fmt"
|
|||
|
|
// "go-admin/config"
|
|||
|
|
// "io"
|
|||
|
|
// "mime/multipart"
|
|||
|
|
// "path"
|
|||
|
|
// "path/filepath"
|
|||
|
|
// "strings"
|
|||
|
|
// "sync"
|
|||
|
|
// "time"
|
|||
|
|
//
|
|||
|
|
// "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|||
|
|
// "github.com/google/uuid"
|
|||
|
|
//)
|
|||
|
|
//
|
|||
|
|
//var (
|
|||
|
|
// scheme = "https"
|
|||
|
|
// clientPool = sync.Pool{}
|
|||
|
|
// once sync.Once
|
|||
|
|
//)
|
|||
|
|
//
|
|||
|
|
//// GetUuidFileName 获取随机文件名
|
|||
|
|
//func GetUuidFileName() string {
|
|||
|
|
// return strings.ReplaceAll(uuid.NewString(), "-", "")
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//func GetDomain() string {
|
|||
|
|
// domain := strings.Join([]string{"https://", config.ExtConfig.ALYOssConfig.BucketName, ".", config.ExtConfig.ALYOssConfig.Endpoint}, "")
|
|||
|
|
// return domain
|
|||
|
|
//
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// PublicUpload oss 公开图片文件上传
|
|||
|
|
//func PublicUpload(fileName string, fileByte []byte) (url string, err error) {
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("oss init err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 上传阿里云路径
|
|||
|
|
// folderName := time.Now().Format("2006-01-02")
|
|||
|
|
// if fileName == "" {
|
|||
|
|
// fileName = fmt.Sprintf("%v.jpg", GetUuidFileName())
|
|||
|
|
// }
|
|||
|
|
// yunFileTmpPath := filepath.Join("uploads", folderName, "coin", fileName)
|
|||
|
|
// // windows文件问题
|
|||
|
|
// yunFileTmpPath = strings.ReplaceAll(yunFileTmpPath, "\\", "/")
|
|||
|
|
//
|
|||
|
|
// // 上传Byte数组
|
|||
|
|
// option := oss.ContentType("image/jpg")
|
|||
|
|
// err = bucket.PutObject(yunFileTmpPath, bytes.NewReader(fileByte), option)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("upload file err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// domain := GetDomain()
|
|||
|
|
// return domain + "/" + yunFileTmpPath, nil
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// UploadVideoOSS 此方法可以用来上传各种类型的文件
|
|||
|
|
//func UploadVideoOSS(file io.Reader, yunFileTmpPath string) (url string, err error) {
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("oss init err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// //option := oss.ContentType("image/jpg") // 支持 jpg/png
|
|||
|
|
// err = bucket.PutObject(yunFileTmpPath, file)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("upload file err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// domain := GetDomain()
|
|||
|
|
// return domain + "/" + yunFileTmpPath, nil
|
|||
|
|
//
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//func PublicUpload1(file io.Reader) (url string, err error) {
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("oss init err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 上传阿里云路径
|
|||
|
|
// folderName := time.Now().Format("2006-01-02")
|
|||
|
|
// yunFileTmpPath := filepath.Join("uploads", folderName, fmt.Sprintf("%v.jpg", GetUuidFileName()))
|
|||
|
|
// // windows文件问题
|
|||
|
|
// yunFileTmpPath = strings.ReplaceAll(yunFileTmpPath, "\\", "/")
|
|||
|
|
//
|
|||
|
|
// // 上传Byte数组
|
|||
|
|
// option := oss.ContentType("image/jpg")
|
|||
|
|
// err = bucket.PutObject(yunFileTmpPath, file, option)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("upload file err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// domain := GetDomain()
|
|||
|
|
// return domain + "/" + yunFileTmpPath, nil
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
////// SecurityUpload 私有图片文件上传
|
|||
|
|
////// TODO 该方法有问题待修改, 请勿使用
|
|||
|
|
////func SecurityUpload(fileName string, fileByte []byte) (url string, err error) {
|
|||
|
|
//// // 临时访问凭证
|
|||
|
|
//// credentials, err := getSecurityToken()
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get security err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 创建OSSClient实例
|
|||
|
|
//// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, credentials.AccessKeyId, credentials.AccessKeySecret, oss.SecurityToken(credentials.SecurityToken))
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("create oss virtual client err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 获取存储空间
|
|||
|
|
//// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 上传阿里云路径
|
|||
|
|
//// folderName := time.Now().Format("2006-01-02")
|
|||
|
|
//// yunFileTmpPath := filepath.Join("uploads", folderName, fmt.Sprintf("f%v_%v.jpg", fileName, GetUuidFileName()))
|
|||
|
|
//// // windows文件问题
|
|||
|
|
//// yunFileTmpPath = strings.ReplaceAll(yunFileTmpPath, "\\", "/")
|
|||
|
|
//// // 带可选参数的签名直传
|
|||
|
|
//// options := []oss.Option{
|
|||
|
|
//// oss.ContentType("image/jpg"),
|
|||
|
|
//// }
|
|||
|
|
//// err = bucket.PutObject(yunFileTmpPath, bytes.NewReader(fileByte), options...)
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("upload file err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// signedGetURL, err := bucket.SignURL(yunFileTmpPath, oss.HTTPGet, utility.StringAsInt64(config.ExtConfig.ALYOssConfig.ExpiredInSec))
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get sign url err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// return signedGetURL, nil
|
|||
|
|
////}
|
|||
|
|
////func GetSecurityURL(fileName string) (url string, err error) {
|
|||
|
|
//// // 临时访问凭证
|
|||
|
|
//// credentials, err := getSecurityToken()
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get security err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 创建OSSClient实例
|
|||
|
|
//// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, credentials.AccessKeyId, credentials.AccessKeySecret, oss.SecurityToken(credentials.SecurityToken))
|
|||
|
|
//// // client, err := oss.New(appconfig.ExtConfig.ALYOssConfig.Endpoint, appconfig.ExtConfig.ALYOssConfig.AccessKeyID, appconfig.ExtConfig.ALYOssConfig.AccessKeySecret, oss.SecurityToken(credentials.SecurityToken))
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("create oss virtual client err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 获取存储空间
|
|||
|
|
//// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 上传阿里云路径
|
|||
|
|
//// folderName := time.Now().Format("2006-01-02")
|
|||
|
|
//// yunFileTmpPath := filepath.Join("uploads", folderName, fileName)
|
|||
|
|
//// // windows文件问题
|
|||
|
|
//// yunFileTmpPath = strings.ReplaceAll(yunFileTmpPath, "\\", "/")
|
|||
|
|
//// // 带可选参数的签名直传
|
|||
|
|
//// fmt.Println("fdfasfsdfasfsdf", yunFileTmpPath)
|
|||
|
|
//// signedGetURL, err := bucket.SignURL(yunFileTmpPath, oss.HTTPGet, utility.StringAsInt64(config.ExtConfig.ALYOssConfig.ExpiredInSec))
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return url, fmt.Errorf("get sign url err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// return signedGetURL, nil
|
|||
|
|
////}
|
|||
|
|
////func getSecurityToken() (credentials sts.Credentials, er error) {
|
|||
|
|
//// // 构建一个阿里云客户端, 用于发起请求。
|
|||
|
|
//// // 构建阿里云客户端时,需要设置AccessKey ID和AccessKey Secret。
|
|||
|
|
//// client, err := sts.NewClientWithAccessKey(config.ExtConfig.ALYOssConfig.RegionId, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// return credentials, fmt.Errorf("get credentials err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// // 构建请求对象。
|
|||
|
|
//// request := sts.CreateAssumeRoleRequest()
|
|||
|
|
//// // 设置参数。关于参数含义和设置方法,请参见《API参考》。
|
|||
|
|
//// request.Scheme = scheme
|
|||
|
|
//// request.RoleArn = config.ExtConfig.ALYOssConfig.RoleArn
|
|||
|
|
//// request.RoleSessionName = config.ExtConfig.ALYOssConfig.RoleSessionName
|
|||
|
|
////
|
|||
|
|
//// // 发起请求,并得到响应。
|
|||
|
|
//// response, err := client.AssumeRole(request)
|
|||
|
|
//// if err != nil {
|
|||
|
|
//// loghelper.Error("get assume role err", zap.Error(err))
|
|||
|
|
//// return credentials, fmt.Errorf("get assume role err: %w", err)
|
|||
|
|
//// }
|
|||
|
|
//// return response.Credentials, nil
|
|||
|
|
////}
|
|||
|
|
//
|
|||
|
|
//type UploadByBase64 struct {
|
|||
|
|
// Images string `json:"images" validate:"required"`
|
|||
|
|
// FileName string `json:"fileName"` // 文件名称包含文件类型
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// UploadByString @name 上传文件-字符串
|
|||
|
|
//func UploadByString(params UploadByBase64) (url string, err error) {
|
|||
|
|
// // 获取文件名称
|
|||
|
|
// if params.FileName == "" {
|
|||
|
|
// // 获取上传文件类型
|
|||
|
|
// fileTypePosition := strings.Index(params.Images, "/")
|
|||
|
|
// fileType := params.Images[fileTypePosition+1 : fileTypePosition+5]
|
|||
|
|
// uid := uuid.NewString()
|
|||
|
|
// params.FileName = uid + "." + fileType // 代码生成图片名称
|
|||
|
|
// }
|
|||
|
|
// filePath := fmt.Sprintf("%v/%v", time.Now().Format("2006-01-02"), params.FileName)
|
|||
|
|
// uploadPath := filepath.Join("uploads", filePath) // 生成oos图片存储路径
|
|||
|
|
//
|
|||
|
|
// // 获取图片内容并base64解密
|
|||
|
|
// fileContentPosition := strings.Index(params.Images, ",")
|
|||
|
|
// uploadBaseString := params.Images[fileContentPosition+1:]
|
|||
|
|
// uploadString, _ := base64.StdEncoding.DecodeString(uploadBaseString)
|
|||
|
|
//
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("create oss client err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// option := oss.ContentType("image/jpg")
|
|||
|
|
// err = bucket.PutObject(uploadPath, strings.NewReader(string(uploadString)), option)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("put object err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// domain := GetDomain()
|
|||
|
|
// return domain + "/" + uploadPath, nil
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// 从连接池中获取客户端实例
|
|||
|
|
//func getClient() (*oss.Client, error) {
|
|||
|
|
// once.Do(func() {
|
|||
|
|
// clientPool.New = func() interface{} {
|
|||
|
|
// if config.ExtConfig.ALYOssConfig.Endpoint == `` || config.ExtConfig.ALYOssConfig.AccessKeyID == `` || config.ExtConfig.ALYOssConfig.AccessKeySecret == `` {
|
|||
|
|
// return fmt.Errorf("阿里云oss配置错误")
|
|||
|
|
// }
|
|||
|
|
// client, err := oss.New(config.ExtConfig.ALYOssConfig.Endpoint, config.ExtConfig.ALYOssConfig.AccessKeyID, config.ExtConfig.ALYOssConfig.AccessKeySecret)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return fmt.Errorf("oss init err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// return client
|
|||
|
|
// }
|
|||
|
|
// })
|
|||
|
|
// poolObj := clientPool.Get()
|
|||
|
|
// client, ok := poolObj.(*oss.Client)
|
|||
|
|
// if !ok {
|
|||
|
|
// err, ok := poolObj.(error)
|
|||
|
|
// if ok {
|
|||
|
|
// return nil, err
|
|||
|
|
// }
|
|||
|
|
// return nil, fmt.Errorf("getClient err: %v", poolObj)
|
|||
|
|
// }
|
|||
|
|
// return client, nil
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// 将实例放回
|
|||
|
|
//func putClient(x *oss.Client) {
|
|||
|
|
// clientPool.Put(x)
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// UploadFromFileHeader 通过multipart.FileHeader上传文件
|
|||
|
|
//func UploadFromFileHeader(fileType string, file *multipart.FileHeader) (url string, err error) {
|
|||
|
|
// fileReader, err := file.Open()
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, err
|
|||
|
|
// }
|
|||
|
|
// filePath := getPath("uploads/"+fileType, path.Ext(file.Filename))
|
|||
|
|
//
|
|||
|
|
// return Upload(fileReader, filePath, oss.ContentType("image/jpg"))
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// getPath 通过路径和文件名后缀获取上传阿里云路径
|
|||
|
|
//func getPath(filePath, ext string) (resPath string) {
|
|||
|
|
// resPath = path.Join(filePath, time.Now().Format("2006-01-02"), fmt.Sprintf("%v.%s", GetUuidFileName(), ext))
|
|||
|
|
// // windows文件问题
|
|||
|
|
// resPath = strings.ReplaceAll(resPath, "\\", "/")
|
|||
|
|
// return resPath
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// deUrl 解开url到阿里云路径 用于删除对象
|
|||
|
|
//func deUrl(url string) (resPath string) {
|
|||
|
|
// return strings.Replace(url, "https://"+config.ExtConfig.ALYOssConfig.BucketName+"."+config.ExtConfig.ALYOssConfig.Endpoint+"/", ``, 1)
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// enUrl 合成url
|
|||
|
|
//func enUrl(path string) (resPath string) {
|
|||
|
|
// return "https://" + config.ExtConfig.ALYOssConfig.BucketName + "." + config.ExtConfig.ALYOssConfig.Endpoint + "/" + path
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// Upload 指定 file文件, path 存储路径 options 配置选项
|
|||
|
|
//func Upload(file io.Reader, path string, options ...oss.Option) (url string, err error) {
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := getClient()
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, err
|
|||
|
|
// }
|
|||
|
|
// defer putClient(client)
|
|||
|
|
//
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 上传Byte数组
|
|||
|
|
// err = bucket.PutObject(path, file, options...)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return url, fmt.Errorf("upload file err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// url = enUrl(path)
|
|||
|
|
// return
|
|||
|
|
//}
|
|||
|
|
//
|
|||
|
|
//// Delete 删除对象
|
|||
|
|
//func Delete(filePath string) (err error) {
|
|||
|
|
// // 创建OSSClient实例
|
|||
|
|
// client, err := getClient()
|
|||
|
|
// if err != nil {
|
|||
|
|
// return err
|
|||
|
|
// }
|
|||
|
|
// defer putClient(client)
|
|||
|
|
//
|
|||
|
|
// // 获取存储空间
|
|||
|
|
// bucket, err := client.Bucket(config.ExtConfig.ALYOssConfig.BucketName)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return fmt.Errorf("get bucket err: %w", err)
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// // 删除
|
|||
|
|
// err = bucket.DeleteObject(deUrl(filePath))
|
|||
|
|
// if err != nil {
|
|||
|
|
// return fmt.Errorf("upload file err: %w", err)
|
|||
|
|
// }
|
|||
|
|
// return
|
|||
|
|
//}
|