:zap:优化

This commit is contained in:
coward
2023-02-27 15:25:33 +08:00
parent c00770cab3
commit aa203fcf61
2 changed files with 66 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
package go_aliyun_oss package go_aliyun_oss
import ( import (
"errors"
"fmt" "fmt"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"io/ioutil" "io/ioutil"
@@ -35,7 +36,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
ossFile.FileByte, err = ioutil.ReadAll(ossFile.File.(*os.File)) ossFile.FileByte, err = ioutil.ReadAll(ossFile.File.(*os.File))
if err != nil { if err != nil {
panic("read os type file failed:" + err.Error()) return nil, errors.New("read os type file failed:" + err.Error())
} }
_, ossFile.FileOldName = filepath.Split(ossFile.File.(*os.File).Name()) _, ossFile.FileOldName = filepath.Split(ossFile.File.(*os.File).Name())
@@ -55,7 +56,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
ossFile.FileByte, err = ioutil.ReadAll(fileResources) ossFile.FileByte, err = ioutil.ReadAll(fileResources)
if err != nil { if err != nil {
panic("read multipart file failed:" + err.Error()) return nil, errors.New("read multipart file failed:" + err.Error())
} }
ossFile.FileOldName = ossFile.File.(*multipart.FileHeader).Filename ossFile.FileOldName = ossFile.File.(*multipart.FileHeader).Filename
@@ -66,7 +67,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
newFile, err := os.Open(ossFile.File.(string)) newFile, err := os.Open(ossFile.File.(string))
if err != nil { if err != nil {
panic("open file path failed:" + err.Error()) return nil, errors.New("open file path failed:" + err.Error())
} }
defer newFile.Close() defer newFile.Close()
@@ -92,7 +93,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
default: default:
fmt.Println(reflect.TypeOf(ossFile.File)) fmt.Println(reflect.TypeOf(ossFile.File))
panic("file type is not support" ) return nil, errors.New("file type is not support")
} }
ossFile.GetFileType() ossFile.GetFileType()

View File

@@ -2,6 +2,7 @@ package go_aliyun_oss
import ( import (
"bytes" "bytes"
"errors"
"github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/aliyun/aliyun-oss-go-sdk/oss"
) )
@@ -22,7 +23,7 @@ type ossResponse struct {
// params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/ // params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/
// params: file interface `upload file resource [文件资源]` // params: file interface `upload file resource [文件资源]`
// return string `oss file accessible uri [可访问地址]` // return string `oss file accessible uri [可访问地址]`
func (client *AliOssClient) Put(ossDir string, file interface{},fileType string) *ossResponse { func (client *AliOssClient) Put(ossDir string, file interface{}, fileType string) (*ossResponse, error) {
//file to []byte //file to []byte
//文件转字节流 //文件转字节流
uploadFile := &OssFile{ uploadFile := &OssFile{
@@ -33,7 +34,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
ossFile, err := uploadFile.FileTypeTransForm() ossFile, err := uploadFile.FileTypeTransForm()
if err != nil { if err != nil {
panic("transfer file failed" + err.Error()) return nil, err
} }
// 最终的oss名称 // 最终的oss名称
@@ -58,7 +59,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
err = client.Client.PutObject(ossPath, bytes.NewReader(ossFile.FileByte)) err = client.Client.PutObject(ossPath, bytes.NewReader(ossFile.FileByte))
if err != nil { if err != nil {
panic("put file to oss failed:" + err.Error()) return nil, errors.New("put file to oss failed:" + err.Error())
} }
return &ossResponse{ return &ossResponse{
@@ -66,59 +67,59 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
LongPath: client.Domain + "/" + ossPath, LongPath: client.Domain + "/" + ossPath,
ShortPath: ossPath, ShortPath: ossPath,
FileName: ossFileName, FileName: ossFileName,
} }, nil
} }
// HasExists 校验文件是否已经存在 // HasExists 校验文件是否已经存在
// check file already exists in oss server // check file already exists in oss server
// params: ossFilePath string `file oss path [文件的oss的路径]` // params: ossFilePath string `file oss path [文件的oss的路径]`
func (client *AliOssClient) HasExists(ossFilePath string) bool { func (client *AliOssClient) HasExists(ossFilePath string) (bool, error) {
//oss check fun //oss check fun
isExists, err := client.Client.IsObjectExist(ossFilePath) isExists, err := client.Client.IsObjectExist(ossFilePath)
if err != nil { if err != nil {
panic("check file in oss is exists failed:" + err.Error()) return false, errors.New("check file in oss is exists failed:" + err.Error())
} }
return isExists return isExists, nil
} }
// Delete 删除文件-单文件删除 // Delete 删除文件-单文件删除
// delete one file in oss // delete one file in oss
// params ossPath string `file oss path [文件的oss路径]` // params ossPath string `file oss path [文件的oss路径]`
// return bool // return bool
func (client *AliOssClient) Delete(ossFilePath string) bool { func (client *AliOssClient) Delete(ossFilePath string) (bool, error) {
//oss delete one file fun //oss delete one file fun
err := client.Client.DeleteObject(ossFilePath) err := client.Client.DeleteObject(ossFilePath)
if err != nil { if err != nil {
panic("delete file "+ ossFilePath +" failed:" + err.Error()) return false, errors.New("delete file " + ossFilePath + " failed:" + err.Error())
} }
return true return true, nil
} }
// DeleteMore 删除文件-多文件删除 // DeleteMore 删除文件-多文件删除
// delete more file in oss // delete more file in oss
// params ossPath []string `file oss path array [文件的oss路径数组]` // params ossPath []string `file oss path array [文件的oss路径数组]`
// return bool // return bool
func (client *AliOssClient) DeleteMore(ossFilePath []string) bool { func (client *AliOssClient) DeleteMore(ossFilePath []string) (bool, error) {
//oss delete more file fun //oss delete more file fun
_, err := client.Client.DeleteObjects(ossFilePath) _, err := client.Client.DeleteObjects(ossFilePath)
if err != nil { if err != nil {
panic("delete more file in oss failed:" + err.Error()) return false, errors.New("delete more file in oss failed:" + err.Error())
} }
return true return true, nil
} }
// GetTemporaryUrl 获取文件临时地址 // GetTemporaryUrl 获取文件临时地址
// path string 文件路径 // path string 文件路径
// expireInSecond int64 多久后过期 单位: 秒,默认 60 // expireInSecond int64 多久后过期 单位: 秒,默认 60
func (client *AliOssClient) GetTemporaryUrl(path string,expireInSecond int64) string { func (client *AliOssClient) GetTemporaryUrl(path string, expireInSecond int64) (string, error) {
var expireTime int64 var expireTime int64
@@ -131,9 +132,9 @@ func (client *AliOssClient) GetTemporaryUrl(path string,expireInSecond int64) st
signUrl, err := client.Client.SignURL(path, oss.HTTPGet, expireTime) signUrl, err := client.Client.SignURL(path, oss.HTTPGet, expireTime)
if err != nil { if err != nil {
panic("generate sign url failed:" + err.Error()) return "", errors.New("generate sign url failed:" + err.Error())
} }
return signUrl return signUrl, nil
} }