Files
go_aliyun_oss/oss_operation.go

141 lines
3.5 KiB
Go
Raw Normal View History

2021-03-13 23:19:41 +08:00
package go_aliyun_oss
2020-11-21 21:51:07 +08:00
import (
"bytes"
2023-02-27 15:25:33 +08:00
"errors"
2020-11-21 21:51:07 +08:00
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
type AliOssClient struct {
2023-02-27 15:25:33 +08:00
Domain string
2020-11-21 21:51:07 +08:00
OriginalFileName bool
2023-02-27 15:25:33 +08:00
Client *oss.Bucket
2020-11-21 21:51:07 +08:00
}
2021-04-07 18:13:24 +08:00
type ossResponse struct {
2023-02-27 15:25:33 +08:00
Host string
LongPath string
2021-04-07 17:33:23 +08:00
ShortPath string
2023-02-27 15:25:33 +08:00
FileName string
2021-04-07 17:33:23 +08:00
}
// Put 推送文件到oss
2023-02-27 15:25:33 +08:00
// 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) {
2020-11-21 21:51:07 +08:00
//file to []byte
//文件转字节流
uploadFile := &OssFile{
2023-02-27 15:25:33 +08:00
File: file,
2021-03-16 11:17:11 +08:00
FileType: fileType,
2020-11-21 21:51:07 +08:00
}
2023-02-27 15:25:33 +08:00
ossFile, err := uploadFile.FileTypeTransForm()
2020-11-21 21:51:07 +08:00
if err != nil {
2023-02-27 15:25:33 +08:00
return nil, err
2020-11-21 21:51:07 +08:00
}
2021-04-07 18:13:24 +08:00
// 最终的oss名称
2021-04-07 17:33:23 +08:00
var ossFileName string
2020-11-21 21:51:07 +08:00
//ossPath = oss dir + upload file name
//example: oss dir is diy ==== test/20201121/
//time.Now().Format("20060102")
//ossPath := path + fileName
var ossPath string
//judge is use origin file name if false fileName = fileNewName (is a only name) else file init name
if client.OriginalFileName == false {
ossPath = ossDir + ossFile.FileNewName
2021-04-07 17:33:23 +08:00
ossFileName = ossFile.FileNewName
2020-11-21 21:51:07 +08:00
} else {
ossPath = ossDir + ossFile.FileOldName
2021-04-07 17:33:23 +08:00
ossFileName = ossFile.FileOldName
2020-11-21 21:51:07 +08:00
}
//upload file to oss
2023-02-27 15:25:33 +08:00
err = client.Client.PutObject(ossPath, bytes.NewReader(ossFile.FileByte))
2020-11-21 21:51:07 +08:00
if err != nil {
2023-02-27 15:25:33 +08:00
return nil, errors.New("put file to oss failed:" + err.Error())
2020-11-21 21:51:07 +08:00
}
2021-04-07 18:13:24 +08:00
return &ossResponse{
2023-02-27 15:25:33 +08:00
Host: client.Domain,
LongPath: client.Domain + "/" + ossPath,
2021-04-07 17:33:23 +08:00
ShortPath: ossPath,
2023-02-27 15:25:33 +08:00
FileName: ossFileName,
}, nil
2020-11-21 21:51:07 +08:00
}
// HasExists 校验文件是否已经存在
2023-02-27 15:25:33 +08:00
// check file already exists in oss server
// params: ossFilePath string `file oss path [文件的oss的路径]`
func (client *AliOssClient) HasExists(ossFilePath string) (bool, error) {
2020-11-21 21:51:07 +08:00
//oss check fun
2023-02-27 15:25:33 +08:00
isExists, err := client.Client.IsObjectExist(ossFilePath)
2020-11-21 21:51:07 +08:00
if err != nil {
2023-02-27 15:25:33 +08:00
return false, errors.New("check file in oss is exists failed:" + err.Error())
2020-11-21 21:51:07 +08:00
}
2023-02-27 15:25:33 +08:00
return isExists, nil
2020-11-21 21:51:07 +08:00
}
// Delete 删除文件-单文件删除
2023-02-27 15:25:33 +08:00
// delete one file in oss
// params ossPath string `file oss path [文件的oss路径]`
// return bool
func (client *AliOssClient) Delete(ossFilePath string) (bool, error) {
2020-11-21 21:51:07 +08:00
//oss delete one file fun
err := client.Client.DeleteObject(ossFilePath)
if err != nil {
2023-02-27 15:25:33 +08:00
return false, errors.New("delete file " + ossFilePath + " failed:" + err.Error())
2020-11-21 21:51:07 +08:00
}
2023-02-27 15:25:33 +08:00
return true, nil
2020-11-21 21:51:07 +08:00
}
// DeleteMore 删除文件-多文件删除
2023-02-27 15:25:33 +08:00
// delete more file in oss
// params ossPath []string `file oss path array [文件的oss路径数组]`
// return bool
func (client *AliOssClient) DeleteMore(ossFilePath []string) (bool, error) {
2020-11-21 21:51:07 +08:00
//oss delete more file fun
2023-02-27 15:25:33 +08:00
_, err := client.Client.DeleteObjects(ossFilePath)
2020-11-21 21:51:07 +08:00
if err != nil {
2023-02-27 15:25:33 +08:00
return false, errors.New("delete more file in oss failed:" + err.Error())
2020-11-21 21:51:07 +08:00
}
2023-02-27 15:25:33 +08:00
return true, nil
}
// GetTemporaryUrl 获取文件临时地址
// path string 文件路径
// expireInSecond int64 多久后过期 单位: 秒,默认 60
2023-02-27 15:25:33 +08:00
func (client *AliOssClient) GetTemporaryUrl(path string, expireInSecond int64) (string, error) {
var expireTime int64
if expireInSecond <= 0 {
expireTime = 60
} else {
expireTime = expireInSecond
}
2023-02-27 15:25:33 +08:00
signUrl, err := client.Client.SignURL(path, oss.HTTPGet, expireTime)
if err != nil {
2023-02-27 15:25:33 +08:00
return "", errors.New("generate sign url failed:" + err.Error())
}
2023-02-27 15:25:33 +08:00
return signUrl, nil
2023-02-27 15:25:33 +08:00
}