Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cd46a45aa | ||
|
|
386d2db45e | ||
|
|
20fae36533 | ||
|
|
605a9f829e | ||
|
|
a794b3d2ff | ||
|
|
d89ca3242c | ||
|
|
17b7856b9b | ||
|
|
cf0e2dc94c |
@@ -5,6 +5,7 @@
|
|||||||
对阿里云oss-golang sdk 进行上传、删除的简单封装,便于使用
|
对阿里云oss-golang sdk 进行上传、删除的简单封装,便于使用
|
||||||
上传使用字节流,内自带 *os.File | *multipart.FileHeader 文件类型转字节流,
|
上传使用字节流,内自带 *os.File | *multipart.FileHeader 文件类型转字节流,
|
||||||
以及使用字符串路径 例如:./test.png 。测试案例oss_test.go中有不同操作的案例
|
以及使用字符串路径 例如:./test.png 。测试案例oss_test.go中有不同操作的案例
|
||||||
|
新版兼容文件[]byte化上传
|
||||||
|
|
||||||
#install
|
#install
|
||||||
go get github.com/cowardmrx/go_aliyun_oss
|
go get github.com/cowardmrx/go_aliyun_oss
|
||||||
@@ -23,8 +24,8 @@ go get github.com/cowardmrx/go_aliyun_oss
|
|||||||
|
|
||||||
ossClient := ossConfig.CreateOssConnect()
|
ossClient := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
//put 方法返回完整的oss 可访问地址
|
//put 方法返回完整的oss 可访问地址 第三个参数 如果文件不是[]byte数组的可以直接传递空,如果是[]byte也可以是空,会默认给出一个png的文件类型,建议实际中给出文件类型
|
||||||
uri := ossClient.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
uri := ossClient.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg","")
|
||||||
fmt.Println(uri)
|
fmt.Println(uri)
|
||||||
|
|
||||||
//HasExists 方法返回一个bool值 true-存在 false-不存在
|
//HasExists 方法返回一个bool值 true-存在 false-不存在
|
||||||
@@ -43,6 +44,4 @@ go get github.com/cowardmrx/go_aliyun_oss
|
|||||||
})
|
})
|
||||||
fmt.Println(isSuccess)
|
fmt.Println(isSuccess)
|
||||||
|
|
||||||
|
#详细使用方法请参照oss_test.go文件中的示例
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
45
file.go
45
file.go
@@ -24,13 +24,17 @@ type OssFileInterface interface {
|
|||||||
GetFileType() *OssFile
|
GetFileType() *OssFile
|
||||||
}
|
}
|
||||||
|
|
||||||
// file type transform
|
// @method: FileTypeTransForm
|
||||||
//@title 文件类型转换
|
// @description: 文件类型转换
|
||||||
|
// @author: mr.x 2021-06-19 00:28:46
|
||||||
|
// @return: *OssFile
|
||||||
|
// @return: error
|
||||||
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
switch ossFile.File.(type) {
|
switch ossFile.File.(type) {
|
||||||
case *os.File:
|
case *os.File:
|
||||||
|
|
||||||
ossFile.FileByte,err = ioutil.ReadAll(ossFile.File.(*os.File))
|
ossFile.FileByte,err = ioutil.ReadAll(ossFile.File.(*os.File))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,6 +43,8 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
|||||||
|
|
||||||
_,ossFile.FileOldName = filepath.Split(ossFile.File.(*os.File).Name())
|
_,ossFile.FileOldName = filepath.Split(ossFile.File.(*os.File).Name())
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
case *multipart.FileHeader:
|
case *multipart.FileHeader:
|
||||||
|
|
||||||
fileResources,err := ossFile.File.(*multipart.FileHeader).Open()
|
fileResources,err := ossFile.File.(*multipart.FileHeader).Open()
|
||||||
@@ -57,6 +63,8 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
|||||||
|
|
||||||
ossFile.FileOldName = ossFile.File.(*multipart.FileHeader).Filename
|
ossFile.FileOldName = ossFile.File.(*multipart.FileHeader).Filename
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
case string:
|
case string:
|
||||||
newFile,err := os.Open(ossFile.File.(string))
|
newFile,err := os.Open(ossFile.File.(string))
|
||||||
|
|
||||||
@@ -70,6 +78,21 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
|||||||
|
|
||||||
_,ossFile.FileOldName = filepath.Split(newFile.Name())
|
_,ossFile.FileOldName = filepath.Split(newFile.Name())
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
|
// 支持[]byte数组传递 因为无法解析文件类型 默认直接给出文件类型为.png
|
||||||
|
case []byte:
|
||||||
|
ossFile.FileByte = ossFile.File.([]byte)
|
||||||
|
|
||||||
|
//判断是否指定了文件的类型 如果没有指定默认为png格式
|
||||||
|
if ossFile.FileType == "" || len(ossFile.FileType) <= 0 {
|
||||||
|
ossFile.FileOldName = uuid.NewV4().String() + ".png"
|
||||||
|
} else {
|
||||||
|
ossFile.FileOldName = uuid.NewV4().String() + ossFile.FileType
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println(reflect.TypeOf(ossFile.File))
|
fmt.Println(reflect.TypeOf(ossFile.File))
|
||||||
panic("file type is not support" )
|
panic("file type is not support" )
|
||||||
@@ -80,15 +103,21 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
|||||||
return ossFile,nil
|
return ossFile,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//split file type and generate file name
|
// @method: GetFileType
|
||||||
//截取文件类型
|
// @description: 截取文件类型
|
||||||
|
// @author: mr.x 2021-06-19 00:28:52
|
||||||
|
// @return: *OssFile
|
||||||
func (ossFile *OssFile) GetFileType() *OssFile {
|
func (ossFile *OssFile) GetFileType() *OssFile {
|
||||||
//from oldFileName split file type
|
|
||||||
fileTypeSufIndex := strings.Index(ossFile.FileOldName,".")
|
|
||||||
|
|
||||||
fileType := ossFile.FileOldName[fileTypeSufIndex:]
|
// 当没有传递文件类型时去文件名中截取出文件类型
|
||||||
|
if ossFile.FileType == "" || len(ossFile.FileType) <= 0 {
|
||||||
|
//from oldFileName split file type
|
||||||
|
fileTypeSufIndex := strings.Index(ossFile.FileOldName,".")
|
||||||
|
|
||||||
ossFile.FileType = fileType
|
fileType := ossFile.FileOldName[fileTypeSufIndex:]
|
||||||
|
|
||||||
|
ossFile.FileType = fileType
|
||||||
|
}
|
||||||
|
|
||||||
//generate only file name
|
//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
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ type AliOssConfigInterface interface {
|
|||||||
GetAccessibleUrl() string
|
GetAccessibleUrl() string
|
||||||
}
|
}
|
||||||
|
|
||||||
//check AliOssConfig value is exists
|
// @method: CheckConfig
|
||||||
|
// @description: 校验配置
|
||||||
|
// @author: mr.x 2021-06-19 00:28:17
|
||||||
func (coon *AliOssConfig) CheckConfig() {
|
func (coon *AliOssConfig) CheckConfig() {
|
||||||
//check endPoint
|
//check endPoint
|
||||||
if coon.EndPoint == "" || len(coon.EndPoint) <= 0 {
|
if coon.EndPoint == "" || len(coon.EndPoint) <= 0 {
|
||||||
@@ -49,8 +51,10 @@ func (coon *AliOssConfig) CheckConfig() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//en: create oss connect client
|
// @method: CreateOssConnect
|
||||||
//创建阿里云oss 链接客户端
|
// @description: 创建阿里云oss 链接客户端
|
||||||
|
// @author: mr.x 2021-06-19 00:28:10
|
||||||
|
// @return: *AliOssClient
|
||||||
func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
||||||
//config check
|
//config check
|
||||||
coon.CheckConfig()
|
coon.CheckConfig()
|
||||||
@@ -82,8 +86,10 @@ func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//get oss accessible url
|
// @method: GetAccessibleUrl
|
||||||
//拼接阿里云oss可访问地址
|
// @description: 拼接阿里云oss可访问地址
|
||||||
|
// @author: mr.x 2021-06-19 00:28:02
|
||||||
|
// @return: string
|
||||||
func (coon *AliOssConfig) GetAccessibleUrl() string {
|
func (coon *AliOssConfig) GetAccessibleUrl() string {
|
||||||
var domain string
|
var domain string
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,27 @@ type AliOssClient struct {
|
|||||||
Client *oss.Bucket
|
Client *oss.Bucket
|
||||||
}
|
}
|
||||||
|
|
||||||
//推送文件到oss
|
type ossResponse struct {
|
||||||
//params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/
|
Host string
|
||||||
//params: file interface `upload file resource [文件资源]`
|
LongPath string
|
||||||
//return string `oss file accessible uri [可访问地址]`
|
ShortPath string
|
||||||
func (client *AliOssClient) Put(ossDir string, file interface{}) string {
|
FileName string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// @method: Put
|
||||||
|
// @description: 推送文件到oss
|
||||||
|
// @author: mr.x 2021-06-19 00:29:08
|
||||||
|
// @param: ossDir string `[要推送到的oss目录]` example: test/20201121/
|
||||||
|
// @param: file interface{} `upload file resource [文件资源]`
|
||||||
|
// @param: fileType string `文件类型`
|
||||||
|
// @return: *ossResponse 返回oss可访问地址等
|
||||||
|
func (client *AliOssClient) Put(ossDir string, file interface{},fileType string) *ossResponse {
|
||||||
//file to []byte
|
//file to []byte
|
||||||
//文件转字节流
|
//文件转字节流
|
||||||
uploadFile := &OssFile{
|
uploadFile := &OssFile{
|
||||||
File: file,
|
File: file,
|
||||||
|
FileType: fileType,
|
||||||
}
|
}
|
||||||
|
|
||||||
ossFile,err := uploadFile.FileTypeTransForm()
|
ossFile,err := uploadFile.FileTypeTransForm()
|
||||||
@@ -28,6 +40,9 @@ func (client *AliOssClient) Put(ossDir string, file interface{}) string {
|
|||||||
panic("transfer file failed" + err.Error())
|
panic("transfer file failed" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 最终的oss名称
|
||||||
|
var ossFileName string
|
||||||
|
|
||||||
//ossPath = oss dir + upload file name
|
//ossPath = oss dir + upload file name
|
||||||
//example: oss dir is diy ==== test/20201121/
|
//example: oss dir is diy ==== test/20201121/
|
||||||
//time.Now().Format("20060102")
|
//time.Now().Format("20060102")
|
||||||
@@ -37,8 +52,10 @@ func (client *AliOssClient) Put(ossDir string, file interface{}) string {
|
|||||||
//judge is use origin file name if false fileName = fileNewName (is a only name) else file init name
|
//judge is use origin file name if false fileName = fileNewName (is a only name) else file init name
|
||||||
if client.OriginalFileName == false {
|
if client.OriginalFileName == false {
|
||||||
ossPath = ossDir + ossFile.FileNewName
|
ossPath = ossDir + ossFile.FileNewName
|
||||||
|
ossFileName = ossFile.FileNewName
|
||||||
} else {
|
} else {
|
||||||
ossPath = ossDir + ossFile.FileOldName
|
ossPath = ossDir + ossFile.FileOldName
|
||||||
|
ossFileName = ossFile.FileOldName
|
||||||
}
|
}
|
||||||
|
|
||||||
//upload file to oss
|
//upload file to oss
|
||||||
@@ -48,12 +65,19 @@ func (client *AliOssClient) Put(ossDir string, file interface{}) string {
|
|||||||
panic("put file to oss failed:" + err.Error())
|
panic("put file to oss failed:" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.Domain + "/" + ossPath
|
return &ossResponse{
|
||||||
|
Host: client.Domain,
|
||||||
|
LongPath: client.Domain + "/" + ossPath,
|
||||||
|
ShortPath: ossPath,
|
||||||
|
FileName: ossFileName,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//校验文件是否已经存在
|
// @method: HasExists
|
||||||
//check file already exists in oss server
|
// @description: 校验文件是否已经存在
|
||||||
//params: ossFilePath string `file oss path [文件的oss的路径]`
|
// @author: mr.x 2021-06-19 00:30:21
|
||||||
|
// @param: ossFilePath string file oss path [文件的oss的路径]
|
||||||
|
// @return: bool
|
||||||
func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
||||||
|
|
||||||
//oss check fun
|
//oss check fun
|
||||||
@@ -66,10 +90,11 @@ func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
|||||||
return isExists
|
return isExists
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除文件-单文件删除
|
// @method: Delete
|
||||||
//delete one file in oss
|
// @description: 删除文件-单文件删除
|
||||||
//params ossPath string `file oss path [文件的oss路径]`
|
// @author: mr.x 2021-06-19 00:30:40
|
||||||
//return bool
|
// @param: ossFilePath string oss 可访问路径
|
||||||
|
// @return: bool true - 删除成功 | false - 删除失败
|
||||||
func (client *AliOssClient) Delete(ossFilePath string) bool {
|
func (client *AliOssClient) Delete(ossFilePath string) bool {
|
||||||
|
|
||||||
//oss delete one file fun
|
//oss delete one file fun
|
||||||
@@ -82,10 +107,11 @@ func (client *AliOssClient) Delete(ossFilePath string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除文件-多文件删除
|
// @method: DeleteMore
|
||||||
//delete more file in oss
|
// @description: 删除文件-多文件删除
|
||||||
//params ossPath []string `file oss path array [文件的oss路径数组]`
|
// @author: mr.x 2021-06-19 00:30:56
|
||||||
//return bool
|
// @param: ossFilePath []string
|
||||||
|
// @return: bool true - 批量删除成功 | false - 批量删除失败
|
||||||
func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
||||||
//oss delete more file fun
|
//oss delete more file fun
|
||||||
_,err := client.Client.DeleteObjects(ossFilePath)
|
_,err := client.Client.DeleteObjects(ossFilePath)
|
||||||
@@ -95,4 +121,31 @@ func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// @method: GetTemporaryUrl
|
||||||
|
// @description: 获取文件临时地址
|
||||||
|
// @author: mr.x 2021-06-19 00:31:34
|
||||||
|
// @param: path string 文件路径【段路径】
|
||||||
|
// @param: expireInSecond int64 有效时间 秒 默认 60S
|
||||||
|
// @return: string
|
||||||
|
func (client *AliOssClient) GetTemporaryUrl(path string,expireInSecond int64) string {
|
||||||
|
|
||||||
|
var expireTime int64
|
||||||
|
|
||||||
|
if expireInSecond <= 0 {
|
||||||
|
expireTime = 60
|
||||||
|
} else {
|
||||||
|
expireTime = expireInSecond
|
||||||
|
}
|
||||||
|
|
||||||
|
signUrl,err := client.Client.SignURL(path,oss.HTTPGet,expireTime)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic("generate sign url failed:" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return signUrl
|
||||||
|
|
||||||
}
|
}
|
||||||
43
oss_test.go
43
oss_test.go
@@ -1,7 +1,9 @@
|
|||||||
package go_aliyun_oss
|
package go_aliyun_oss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,11 +17,35 @@ func TestPut(t *testing.T) {
|
|||||||
|
|
||||||
client := ossConfig.CreateOssConnect()
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
uri := client.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
uri := client.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg",".png")
|
||||||
|
|
||||||
fmt.Println(uri)
|
fmt.Println(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPutBase64(t *testing.T) {
|
||||||
|
ossConfig := &AliOssConfig{
|
||||||
|
EndPoint: "",
|
||||||
|
AccessKeyId: "",
|
||||||
|
AccessKeySecret: "",
|
||||||
|
BucketName: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
|
// 读取base
|
||||||
|
file,_ := ioutil.ReadFile("./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
||||||
|
|
||||||
|
|
||||||
|
fileStr := base64.StdEncoding.EncodeToString(file)
|
||||||
|
|
||||||
|
bat,_ := base64.StdEncoding.DecodeString(fileStr)
|
||||||
|
|
||||||
|
|
||||||
|
uri := client.Put("logo/",bat,".png")
|
||||||
|
//
|
||||||
|
fmt.Println(uri)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExists(t *testing.T) {
|
func TestExists(t *testing.T) {
|
||||||
ossConfig := &AliOssConfig{
|
ossConfig := &AliOssConfig{
|
||||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||||
@@ -68,3 +94,18 @@ func TestAliOssClient_DeleteMore(t *testing.T) {
|
|||||||
fmt.Println(deleteRes)
|
fmt.Println(deleteRes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAliOssClient_GetTemporaryUrl(t *testing.T) {
|
||||||
|
ossConfig := &AliOssConfig{
|
||||||
|
EndPoint: "",
|
||||||
|
AccessKeyId: "",
|
||||||
|
AccessKeySecret: "",
|
||||||
|
BucketName: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
|
singUrl := client.GetTemporaryUrl("logo/8497b913-2a79-58a1-984b-c25827f8212e.png",180)
|
||||||
|
|
||||||
|
fmt.Println(singUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user