Compare commits
1 Commits
v1.0.5-bet
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cd46a45aa |
13
file.go
13
file.go
@@ -24,8 +24,11 @@ type OssFileInterface interface {
|
||||
GetFileType() *OssFile
|
||||
}
|
||||
|
||||
// FileTypeTransForm file type transform
|
||||
//@title 文件类型转换
|
||||
// @method: FileTypeTransForm
|
||||
// @description: 文件类型转换
|
||||
// @author: mr.x 2021-06-19 00:28:46
|
||||
// @return: *OssFile
|
||||
// @return: error
|
||||
func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
var err error
|
||||
|
||||
@@ -100,8 +103,10 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) {
|
||||
return ossFile,nil
|
||||
}
|
||||
|
||||
// GetFileType 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 {
|
||||
|
||||
// 当没有传递文件类型时去文件名中截取出文件类型
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/cowardmrx/go_aliyun_oss
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.1.5+incompatible
|
||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -1,7 +1,5 @@
|
||||
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.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/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
|
||||
@@ -20,7 +20,9 @@ type AliOssConfigInterface interface {
|
||||
GetAccessibleUrl() string
|
||||
}
|
||||
|
||||
// CheckConfig check AliOssConfig value is exists
|
||||
// @method: CheckConfig
|
||||
// @description: 校验配置
|
||||
// @author: mr.x 2021-06-19 00:28:17
|
||||
func (coon *AliOssConfig) CheckConfig() {
|
||||
//check endPoint
|
||||
if coon.EndPoint == "" || len(coon.EndPoint) <= 0 {
|
||||
@@ -49,8 +51,10 @@ func (coon *AliOssConfig) CheckConfig() {
|
||||
|
||||
}
|
||||
|
||||
// CreateOssConnect en: create oss connect client
|
||||
//创建阿里云oss 链接客户端
|
||||
// @method: CreateOssConnect
|
||||
// @description: 创建阿里云oss 链接客户端
|
||||
// @author: mr.x 2021-06-19 00:28:10
|
||||
// @return: *AliOssClient
|
||||
func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
||||
//config check
|
||||
coon.CheckConfig()
|
||||
@@ -82,8 +86,10 @@ func (coon *AliOssConfig) CreateOssConnect() *AliOssClient {
|
||||
}
|
||||
}
|
||||
|
||||
// GetAccessibleUrl get oss accessible url
|
||||
//拼接阿里云oss可访问地址
|
||||
// @method: GetAccessibleUrl
|
||||
// @description: 拼接阿里云oss可访问地址
|
||||
// @author: mr.x 2021-06-19 00:28:02
|
||||
// @return: string
|
||||
func (coon *AliOssConfig) GetAccessibleUrl() string {
|
||||
var domain string
|
||||
|
||||
|
||||
@@ -18,10 +18,14 @@ type ossResponse struct {
|
||||
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 [可访问地址]`
|
||||
|
||||
// @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
|
||||
//文件转字节流
|
||||
@@ -69,9 +73,11 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string)
|
||||
}
|
||||
}
|
||||
|
||||
// HasExists 校验文件是否已经存在
|
||||
//check file already exists in oss server
|
||||
//params: ossFilePath string `file oss path [文件的oss的路径]`
|
||||
// @method: HasExists
|
||||
// @description: 校验文件是否已经存在
|
||||
// @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 {
|
||||
|
||||
//oss check fun
|
||||
@@ -84,10 +90,11 @@ func (client *AliOssClient) HasExists(ossFilePath string) bool {
|
||||
return isExists
|
||||
}
|
||||
|
||||
// Delete 删除文件-单文件删除
|
||||
//delete one file in oss
|
||||
//params ossPath string `file oss path [文件的oss路径]`
|
||||
//return bool
|
||||
// @method: Delete
|
||||
// @description: 删除文件-单文件删除
|
||||
// @author: mr.x 2021-06-19 00:30:40
|
||||
// @param: ossFilePath string oss 可访问路径
|
||||
// @return: bool true - 删除成功 | false - 删除失败
|
||||
func (client *AliOssClient) Delete(ossFilePath string) bool {
|
||||
|
||||
//oss delete one file fun
|
||||
@@ -100,10 +107,11 @@ func (client *AliOssClient) Delete(ossFilePath string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteMore 删除文件-多文件删除
|
||||
//delete more file in oss
|
||||
//params ossPath []string `file oss path array [文件的oss路径数组]`
|
||||
//return bool
|
||||
// @method: DeleteMore
|
||||
// @description: 删除文件-多文件删除
|
||||
// @author: mr.x 2021-06-19 00:30:56
|
||||
// @param: ossFilePath []string
|
||||
// @return: bool true - 批量删除成功 | false - 批量删除失败
|
||||
func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
||||
//oss delete more file fun
|
||||
_,err := client.Client.DeleteObjects(ossFilePath)
|
||||
@@ -115,9 +123,13 @@ func (client *AliOssClient) DeleteMore(ossFilePath []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// GetTemporaryUrl 获取文件临时地址
|
||||
// path string 文件路径
|
||||
// expireInSecond int64 多久后过期 单位: 秒,默认 60
|
||||
|
||||
// @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
|
||||
|
||||
55
oss_test.go
55
oss_test.go
@@ -7,49 +7,51 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPut(t *testing.T) {
|
||||
func TestPut(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
|
||||
uri := client.Put("logo/", "./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg", ".png")
|
||||
uri := client.Put("logo/","./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg",".png")
|
||||
|
||||
fmt.Println(uri)
|
||||
}
|
||||
|
||||
func TestPutBase64(t *testing.T) {
|
||||
func TestPutBase64(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
|
||||
// 读取base
|
||||
file, _ := ioutil.ReadFile("./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
||||
file,_ := ioutil.ReadFile("./File/3HaqWaOzJWD86DDvZD9Pmn9VUEOBOBbuWackGOXb (2).jpeg")
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
func TestExists(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
@@ -61,10 +63,10 @@ func TestExists(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
@@ -76,10 +78,10 @@ func TestDelete(t *testing.T) {
|
||||
|
||||
func TestAliOssClient_DeleteMore(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "oss-cn-shenzhen.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
@@ -94,15 +96,16 @@ func TestAliOssClient_DeleteMore(t *testing.T) {
|
||||
|
||||
func TestAliOssClient_GetTemporaryUrl(t *testing.T) {
|
||||
ossConfig := &AliOssConfig{
|
||||
EndPoint: "https://oss-cn-hangzhou.aliyuncs.com",
|
||||
AccessKeyId: "",
|
||||
EndPoint: "",
|
||||
AccessKeyId: "",
|
||||
AccessKeySecret: "",
|
||||
BucketName: "",
|
||||
BucketName: "",
|
||||
}
|
||||
|
||||
client := ossConfig.CreateOssConnect()
|
||||
|
||||
singUrl := client.GetTemporaryUrl("", 180)
|
||||
singUrl := client.GetTemporaryUrl("logo/8497b913-2a79-58a1-984b-c25827f8212e.png",180)
|
||||
|
||||
fmt.Println(singUrl)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user