diff --git a/README.md b/README.md index 6f8e0e0..6c65994 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,4 @@ go get github.com/cowardmrx/go_aliyun_oss }) fmt.Println(isSuccess) - - - +#详细使用方法请参照oss_test.go文件中的示例 diff --git a/file.go b/file.go index b3eb98b..72aebb3 100644 --- a/file.go +++ b/file.go @@ -24,7 +24,7 @@ type OssFileInterface interface { GetFileType() *OssFile } -// file type transform +// FileTypeTransForm file type transform //@title 文件类型转换 func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) { var err error @@ -100,7 +100,7 @@ func (ossFile *OssFile) FileTypeTransForm() (*OssFile,error) { return ossFile,nil } -//split file type and generate file name +// GetFileType split file type and generate file name //截取文件类型 func (ossFile *OssFile) GetFileType() *OssFile { diff --git a/oss_client.go b/oss_client.go index e0fdc1d..8dfba18 100644 --- a/oss_client.go +++ b/oss_client.go @@ -20,7 +20,7 @@ type AliOssConfigInterface interface { GetAccessibleUrl() string } -//check AliOssConfig value is exists +// CheckConfig check AliOssConfig value is exists func (coon *AliOssConfig) CheckConfig() { //check endPoint 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 链接客户端 func (coon *AliOssConfig) CreateOssConnect() *AliOssClient { //config check @@ -82,7 +82,7 @@ func (coon *AliOssConfig) CreateOssConnect() *AliOssClient { } } -//get oss accessible url +// GetAccessibleUrl get oss accessible url //拼接阿里云oss可访问地址 func (coon *AliOssConfig) GetAccessibleUrl() string { var domain string diff --git a/oss_operation.go b/oss_operation.go index b461d88..64ddb1a 100644 --- a/oss_operation.go +++ b/oss_operation.go @@ -11,7 +11,7 @@ type AliOssClient struct { Client *oss.Bucket } -//推送文件到oss +// Put 推送文件到oss //params: ossDir string `oss dir [要推送到的oss目录]` example: test/20201121/ //params: file interface `upload file resource [文件资源]` //return string `oss file accessible uri [可访问地址]` @@ -52,7 +52,7 @@ func (client *AliOssClient) Put(ossDir string, file interface{},fileType string) return client.Domain + "/" + ossPath } -//校验文件是否已经存在 +// HasExists 校验文件是否已经存在 //check file already exists in oss server //params: ossFilePath string `file oss path [文件的oss的路径]` func (client *AliOssClient) HasExists(ossFilePath string) bool { @@ -67,7 +67,7 @@ func (client *AliOssClient) HasExists(ossFilePath string) bool { return isExists } -//删除文件-单文件删除 +// Delete 删除文件-单文件删除 //delete one file in oss //params ossPath string `file oss path [文件的oss路径]` //return bool @@ -83,7 +83,7 @@ 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 @@ -96,4 +96,27 @@ func (client *AliOssClient) DeleteMore(ossFilePath []string) bool { } 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 + } \ No newline at end of file diff --git a/oss_test.go b/oss_test.go index 42db67a..b7ccacb 100644 --- a/oss_test.go +++ b/oss_test.go @@ -94,3 +94,18 @@ func TestAliOssClient_DeleteMore(t *testing.T) { 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) +} +