奇淼(piexlmax
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 3 deletions
-
9server/config.yaml
-
7server/config/config.go
-
9server/config/oss.go
-
2server/go.mod
-
74server/utils/upload/aliyun_oss.go
@ -0,0 +1,74 @@ |
|||
package upload |
|||
|
|||
import ( |
|||
"errors" |
|||
"gin-vue-admin/global" |
|||
"github.com/aliyun/aliyun-oss-go-sdk/oss" |
|||
"go.uber.org/zap" |
|||
"mime/multipart" |
|||
"path/filepath" |
|||
"time" |
|||
) |
|||
|
|||
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()) |
|||
} |
|||
|
|||
//上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
|
|||
yunFileTmpPath := filepath.Join("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 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue