:zap:优化
This commit is contained in:
45
file.go
45
file.go
@@ -1,6 +1,7 @@
|
||||
package go_aliyun_oss
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"io/ioutil"
|
||||
@@ -12,39 +13,39 @@ import (
|
||||
)
|
||||
|
||||
type OssFile struct {
|
||||
FileByte []byte
|
||||
FileByte []byte
|
||||
FileOldName string
|
||||
FileNewName string
|
||||
FileType string
|
||||
File interface{}
|
||||
FileType string
|
||||
File interface{}
|
||||
}
|
||||
|
||||
type OssFileInterface interface {
|
||||
FileTypeTransForm() (*OssFile,error)
|
||||
FileTypeTransForm() (*OssFile, error)
|
||||
GetFileType() *OssFile
|
||||
}
|
||||
|
||||
// FileTypeTransForm file type transform
|
||||
//@title 文件类型转换
|
||||
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
// @title 文件类型转换
|
||||
func (ossFile *OssFile) FileTypeTransForm() (*OssFile, error) {
|
||||
var err error
|
||||
|
||||
switch ossFile.File.(type) {
|
||||
case *os.File:
|
||||
|
||||
ossFile.FileByte,err = ioutil.ReadAll(ossFile.File.(*os.File))
|
||||
ossFile.FileByte, err = ioutil.ReadAll(ossFile.File.(*os.File))
|
||||
|
||||
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())
|
||||
|
||||
break
|
||||
|
||||
case *multipart.FileHeader:
|
||||
|
||||
fileResources,err := ossFile.File.(*multipart.FileHeader).Open()
|
||||
fileResources, err := ossFile.File.(*multipart.FileHeader).Open()
|
||||
|
||||
if err != nil {
|
||||
panic("open multipart file failed:" + err.Error())
|
||||
@@ -52,10 +53,10 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
|
||||
defer fileResources.Close()
|
||||
|
||||
ossFile.FileByte,err = ioutil.ReadAll(fileResources)
|
||||
ossFile.FileByte, err = ioutil.ReadAll(fileResources)
|
||||
|
||||
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
|
||||
@@ -63,17 +64,17 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
break
|
||||
|
||||
case string:
|
||||
newFile,err := os.Open(ossFile.File.(string))
|
||||
newFile, err := os.Open(ossFile.File.(string))
|
||||
|
||||
if err != nil {
|
||||
panic("open file path failed:" + err.Error())
|
||||
return nil, errors.New("open file path failed:" + err.Error())
|
||||
}
|
||||
|
||||
defer newFile.Close()
|
||||
|
||||
ossFile.FileByte,err = ioutil.ReadAll(newFile)
|
||||
ossFile.FileByte, err = ioutil.ReadAll(newFile)
|
||||
|
||||
_,ossFile.FileOldName = filepath.Split(newFile.Name())
|
||||
_, ossFile.FileOldName = filepath.Split(newFile.Name())
|
||||
|
||||
break
|
||||
|
||||
@@ -92,22 +93,22 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
|
||||
default:
|
||||
fmt.Println(reflect.TypeOf(ossFile.File))
|
||||
panic("file type is not support" )
|
||||
return nil, errors.New("file type is not support")
|
||||
}
|
||||
|
||||
ossFile.GetFileType()
|
||||
|
||||
return ossFile,nil
|
||||
return ossFile, nil
|
||||
}
|
||||
|
||||
// GetFileType split file type and generate file name
|
||||
//截取文件类型
|
||||
// 截取文件类型
|
||||
func (ossFile *OssFile) GetFileType() *OssFile {
|
||||
|
||||
// 当没有传递文件类型时去文件名中截取出文件类型
|
||||
if ossFile.FileType == "" || len(ossFile.FileType) <= 0 {
|
||||
//from oldFileName split file type
|
||||
fileTypeSufIndex := strings.Index(ossFile.FileOldName,".")
|
||||
fileTypeSufIndex := strings.Index(ossFile.FileOldName, ".")
|
||||
|
||||
fileType := ossFile.FileOldName[fileTypeSufIndex:]
|
||||
|
||||
@@ -115,7 +116,7 @@ func (ossFile *OssFile) GetFileType() *OssFile {
|
||||
}
|
||||
|
||||
//generate only file name
|
||||
ossFile.FileNewName = uuid.NewV5(uuid.NewV4(),ossFile.FileOldName).String() + ossFile.FileType
|
||||
ossFile.FileNewName = uuid.NewV5(uuid.NewV4(), ossFile.FileOldName).String() + ossFile.FileType
|
||||
|
||||
return ossFile
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,38 +2,39 @@ package go_aliyun_oss
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
)
|
||||
|
||||
type AliOssClient struct {
|
||||
Domain string
|
||||
Domain string
|
||||
OriginalFileName bool
|
||||
Client *oss.Bucket
|
||||
Client *oss.Bucket
|
||||
}
|
||||
|
||||
type ossResponse struct {
|
||||
Host string
|
||||
LongPath string
|
||||
Host string
|
||||
LongPath string
|
||||
ShortPath string
|
||||
FileName string
|
||||
FileName string
|
||||
}
|
||||
|
||||
// Put 推送文件到oss
|
||||
//params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/
|
||||
//params: file interface `upload file resource [文件资源]`
|
||||
//return string `oss file accessible uri [可访问地址]`
|
||||
func (client *AliOssClient) Put(ossDir string, file interface{},fileType string) *ossResponse {
|
||||
// params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/
|
||||
// params: file interface `upload file resource [文件资源]`
|
||||
// return string `oss file accessible uri [可访问地址]`
|
||||
func (client *AliOssClient) Put(ossDir string, file interface{}, fileType string) (*ossResponse, error) {
|
||||
//file to []byte
|
||||
//文件转字节流
|
||||
uploadFile := &OssFile{
|
||||
File: file,
|
||||
File: file,
|
||||
FileType: fileType,
|
||||
}
|
||||
|
||||
ossFile,err := uploadFile.FileTypeTransForm()
|
||||
ossFile, err := uploadFile.FileTypeTransForm()
|
||||
|
||||
if err != nil {
|
||||
panic("transfer file failed" + err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 最终的oss名称
|
||||
@@ -55,70 +56,70 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
|
||||
}
|
||||
|
||||
//upload file to oss
|
||||
err = client.Client.PutObject(ossPath,bytes.NewReader(ossFile.FileByte))
|
||||
err = client.Client.PutObject(ossPath, bytes.NewReader(ossFile.FileByte))
|
||||
|
||||
if err != nil {
|
||||
panic("put file to oss failed:" + err.Error())
|
||||
return nil, errors.New("put file to oss failed:" + err.Error())
|
||||
}
|
||||
|
||||
return &ossResponse{
|
||||
Host: client.Domain,
|
||||
LongPath: client.Domain + "/" + ossPath,
|
||||
Host: client.Domain,
|
||||
LongPath: client.Domain + "/" + ossPath,
|
||||
ShortPath: ossPath,
|
||||
FileName: ossFileName,
|
||||
}
|
||||
FileName: ossFileName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// HasExists 校验文件是否已经存在
|
||||
//check file already exists in oss server
|
||||
//params: ossFilePath string `file oss path [文件的oss的路径]`
|
||||
func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
||||
// check file already exists in oss server
|
||||
// params: ossFilePath string `file oss path [文件的oss的路径]`
|
||||
func (client *AliOssClient) HasExists(ossFilePath string) (bool, error) {
|
||||
|
||||
//oss check fun
|
||||
isExists,err := client.Client.IsObjectExist(ossFilePath)
|
||||
isExists, err := client.Client.IsObjectExist(ossFilePath)
|
||||
|
||||
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 one file in oss
|
||||
//params ossPath string `file oss path [文件的oss路径]`
|
||||
//return bool
|
||||
func (client *AliOssClient) Delete(ossFilePath string) bool {
|
||||
// delete one file in oss
|
||||
// params ossPath string `file oss path [文件的oss路径]`
|
||||
// return bool
|
||||
func (client *AliOssClient) Delete(ossFilePath string) (bool, error) {
|
||||
|
||||
//oss delete one file fun
|
||||
err := client.Client.DeleteObject(ossFilePath)
|
||||
|
||||
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 删除文件-多文件删除
|
||||
//delete more file in oss
|
||||
//params ossPath []string `file oss path array [文件的oss路径数组]`
|
||||
//return bool
|
||||
func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
||||
// delete more file in oss
|
||||
// params ossPath []string `file oss path array [文件的oss路径数组]`
|
||||
// return bool
|
||||
func (client *AliOssClient) DeleteMore(ossFilePath []string) (bool, error) {
|
||||
//oss delete more file fun
|
||||
_,err := client.Client.DeleteObjects(ossFilePath)
|
||||
_, err := client.Client.DeleteObjects(ossFilePath)
|
||||
|
||||
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 获取文件临时地址
|
||||
// path string 文件路径
|
||||
// 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
|
||||
|
||||
@@ -128,12 +129,12 @@ func (client *AliOssClient) GetTemporaryUrl(path string,expireInSecond int64) st
|
||||
expireTime = expireInSecond
|
||||
}
|
||||
|
||||
signUrl,err := client.Client.SignURL(path,oss.HTTPGet,expireTime)
|
||||
signUrl, err := client.Client.SignURL(path, oss.HTTPGet, expireTime)
|
||||
|
||||
if err != nil {
|
||||
panic("generate sign url failed:" + err.Error())
|
||||
return "", errors.New("generate sign url failed:" + err.Error())
|
||||
}
|
||||
|
||||
return signUrl
|
||||
return signUrl, nil
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user