123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package upload
- import (
- "errors"
- "mime/multipart"
- "time"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "github.com/flipped-aurora/gin-vue-admin/server/global"
- "go.uber.org/zap"
- )
- type AliyunOSS struct{}
- func (*AliyunOSS) UploadFile(file *multipart.FileHeader) (string, string, error) {
- bucket, err := NewBucket()
- if err != nil {
- global.GVA_LOG.Error("function AliyunOSS.NewBucket() Failed", zap.Any("err", err.Error()))
- return "", "", errors.New("function AliyunOSS.NewBucket() Failed, err:" + err.Error())
- }
- // 读取本地文件。
- f, openError := file.Open()
- if openError != nil {
- global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
- return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
- }
- defer f.Close() // 创建文件 defer 关闭
- // 上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
- //yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
- yunFileTmpPath := global.GVA_CONFIG.AliyunOSS.BasePath + "/" + "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + file.Filename
- // 上传文件流。
- err = bucket.PutObject(yunFileTmpPath, f)
- if err != nil {
- global.GVA_LOG.Error("function formUploader.Put() Failed", zap.Any("err", err.Error()))
- return "", "", errors.New("function formUploader.Put() Failed, err:" + err.Error())
- }
- return global.GVA_CONFIG.AliyunOSS.BucketUrl + "/" + yunFileTmpPath, yunFileTmpPath, nil
- }
- func (*AliyunOSS) DeleteFile(key string) error {
- bucket, err := NewBucket()
- if err != nil {
- global.GVA_LOG.Error("function AliyunOSS.NewBucket() Failed", zap.Any("err", err.Error()))
- return errors.New("function AliyunOSS.NewBucket() Failed, err:" + err.Error())
- }
- // 删除单个文件。objectName表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
- // 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
- err = bucket.DeleteObject(key)
- if err != nil {
- global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
- return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
- }
- return nil
- }
- func NewBucket() (*oss.Bucket, error) {
- // 创建OSSClient实例。
- client, err := oss.New(global.GVA_CONFIG.AliyunOSS.Endpoint, global.GVA_CONFIG.AliyunOSS.AccessKeyId, global.GVA_CONFIG.AliyunOSS.AccessKeySecret)
- if err != nil {
- return nil, err
- }
- // 获取存储空间。
- bucket, err := client.Bucket(global.GVA_CONFIG.AliyunOSS.BucketName)
- if err != nil {
- return nil, err
- }
- return bucket, nil
- }
|