Compare commits
4 Commits
v1.0.3
...
v1.0.5-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c00770cab3 | ||
|
|
386d2db45e | ||
|
|
20fae36533 | ||
|
|
605a9f829e |
@@ -44,6 +44,4 @@ go get github.com/cowardmrx/go_aliyun_oss
|
|||||||
})
|
})
|
||||||
fmt.Println(isSuccess)
|
fmt.Println(isSuccess)
|
||||||
|
|
||||||
|
#详细使用方法请参照oss_test.go文件中的示例
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
file.go
4
file.go
@@ -24,7 +24,7 @@ type OssFileInterface interface {
|
|||||||
GetFileType() *OssFile
|
GetFileType() *OssFile
|
||||||
}
|
}
|
||||||
|
|
||||||
// file type transform
|
// FileTypeTransForm file type transform
|
||||||
//@title 文件类型转换
|
//@title 文件类型转换
|
||||||
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||||
var err error
|
var err error
|
||||||
@@ -100,7 +100,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
|||||||
return ossFile,nil
|
return ossFile,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//split file type and generate file name
|
// GetFileType split file type and generate file name
|
||||||
//截取文件类型
|
//截取文件类型
|
||||||
func (ossFile *OssFile) GetFileType() *OssFile {
|
func (ossFile *OssFile) GetFileType() *OssFile {
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/cowardmrx/go_aliyun_oss
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible
|
github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible
|
||||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
||||||
github.com/satori/go.uuid v1.2.0
|
github.com/satori/go.uuid v1.2.0
|
||||||
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
|
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,5 +1,7 @@
|
|||||||
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible h1:v5yDfjkRY/kOxu05gkh0/D/2wYxbTFCoTr3JqFI0FLE=
|
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible h1:v5yDfjkRY/kOxu05gkh0/D/2wYxbTFCoTr3JqFI0FLE=
|
||||||
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
|
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
|
||||||
|
github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible h1:QoRMR0TCctLDqBCMyOu1eXdZyMw3F7uGA9qPn2J4+R8=
|
||||||
|
github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
|
||||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA=
|
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA=
|
||||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
|
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type AliOssConfigInterface interface {
|
|||||||
GetAccessibleUrl() string
|
GetAccessibleUrl() string
|
||||||
}
|
}
|
||||||
|
|
||||||
//check AliOssConfig value is exists
|
// CheckConfig check AliOssConfig value is exists
|
||||||
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,7 +49,7 @@ func (coon *AliOssConfig) CheckConfig() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//en: create oss connect client
|
// CreateOssConnect en: create oss connect client
|
||||||
//创建阿里云oss 链接客户端
|
//创建阿里云oss 链接客户端
|
||||||
func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
||||||
//config check
|
//config check
|
||||||
@@ -82,7 +82,7 @@ func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//get oss accessible url
|
// GetAccessibleUrl get oss accessible url
|
||||||
//拼接阿里云oss可访问地址
|
//拼接阿里云oss可访问地址
|
||||||
func (coon *AliOssConfig) GetAccessibleUrl() string {
|
func (coon *AliOssConfig) GetAccessibleUrl() string {
|
||||||
var domain string
|
var domain string
|
||||||
|
|||||||
@@ -11,18 +11,18 @@ type AliOssClient struct {
|
|||||||
Client *oss.Bucket
|
Client *oss.Bucket
|
||||||
}
|
}
|
||||||
|
|
||||||
type OssResponse struct {
|
type ossResponse struct {
|
||||||
|
Host string
|
||||||
LongPath string
|
LongPath string
|
||||||
ShortPath string
|
ShortPath string
|
||||||
FileName string
|
FileName string
|
||||||
Host string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//推送文件到oss
|
// Put 推送文件到oss
|
||||||
//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 {
|
||||||
//file to []byte
|
//file to []byte
|
||||||
//文件转字节流
|
//文件转字节流
|
||||||
uploadFile := &OssFile{
|
uploadFile := &OssFile{
|
||||||
@@ -36,6 +36,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
|
|||||||
panic("transfer file failed" + err.Error())
|
panic("transfer file failed" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 最终的oss名称
|
||||||
var ossFileName string
|
var ossFileName string
|
||||||
|
|
||||||
//ossPath = oss dir + upload file name
|
//ossPath = oss dir + upload file name
|
||||||
@@ -60,7 +61,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
|
|||||||
panic("put file to oss failed:" + err.Error())
|
panic("put file to oss failed:" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &OssResponse{
|
return &ossResponse{
|
||||||
Host: client.Domain,
|
Host: client.Domain,
|
||||||
LongPath: client.Domain + "/" + ossPath,
|
LongPath: client.Domain + "/" + ossPath,
|
||||||
ShortPath: ossPath,
|
ShortPath: ossPath,
|
||||||
@@ -68,7 +69,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//校验文件是否已经存在
|
// 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 {
|
||||||
@@ -83,7 +84,7 @@ func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
|||||||
return isExists
|
return isExists
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除文件-单文件删除
|
// 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
|
||||||
@@ -99,7 +100,7 @@ func (client *AliOssClient) Delete(ossFilePath string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除文件-多文件删除
|
// 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
|
||||||
@@ -113,3 +114,26 @@ func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTemporaryUrl 获取文件临时地址
|
||||||
|
// path string 文件路径
|
||||||
|
// expireInSecond int64 多久后过期 单位: 秒,默认 60
|
||||||
|
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
|
||||||
|
|
||||||
|
}
|
||||||
24
oss_test.go
24
oss_test.go
@@ -17,7 +17,7 @@ func TestPut(t *testing.T) {
|
|||||||
|
|
||||||
client := ossConfig.CreateOssConnect()
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
uri := client.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg",".png")
|
uri := client.Put("logo/", "./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg", ".png")
|
||||||
|
|
||||||
fmt.Println(uri)
|
fmt.Println(uri)
|
||||||
}
|
}
|
||||||
@@ -33,15 +33,13 @@ func TestPutBase64(t *testing.T) {
|
|||||||
client := ossConfig.CreateOssConnect()
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
// 读取base
|
// 读取base
|
||||||
file,_ := ioutil.ReadFile("./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
file, _ := ioutil.ReadFile("./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
||||||
|
|
||||||
|
|
||||||
fileStr := base64.StdEncoding.EncodeToString(file)
|
fileStr := base64.StdEncoding.EncodeToString(file)
|
||||||
|
|
||||||
bat,_ := base64.StdEncoding.DecodeString(fileStr)
|
bat, _ := base64.StdEncoding.DecodeString(fileStr)
|
||||||
|
|
||||||
|
uri := client.Put("logo/", bat, ".png")
|
||||||
uri := client.Put("logo/",bat,".png")
|
|
||||||
//
|
//
|
||||||
fmt.Println(uri)
|
fmt.Println(uri)
|
||||||
}
|
}
|
||||||
@@ -94,3 +92,17 @@ func TestAliOssClient_DeleteMore(t *testing.T) {
|
|||||||
fmt.Println(deleteRes)
|
fmt.Println(deleteRes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAliOssClient_GetTemporaryUrl(t *testing.T) {
|
||||||
|
ossConfig := &AliOssConfig{
|
||||||
|
EndPoint: "https://oss-cn-hangzhou.aliyuncs.com",
|
||||||
|
AccessKeyId: "",
|
||||||
|
AccessKeySecret: "",
|
||||||
|
BucketName: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ossConfig.CreateOssConnect()
|
||||||
|
|
||||||
|
singUrl := client.GetTemporaryUrl("", 180)
|
||||||
|
|
||||||
|
fmt.Println(singUrl)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user