Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
aee7202752 | ||
![]() |
5f9f144ab3 | ||
![]() |
e246123ce2 | ||
![]() |
1e2d00edaf | ||
![]() |
0c7ec61e65 |
@@ -22,6 +22,7 @@ type ALiYunOSSClient struct {
|
||||
ossClient *oss.Client
|
||||
}
|
||||
|
||||
// NewAliYunOSS 创建阿里云OSS客户端
|
||||
func (c *ALiYunOSSClient) NewAliYunOSS() (err error) {
|
||||
if c.AccessKeyID == "" || c.AccessKeySecret == "" {
|
||||
return errors.New("请配置 oss accessKeyID accessKeySecret")
|
||||
@@ -35,14 +36,14 @@ func (c *ALiYunOSSClient) NewAliYunOSS() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ALiYunOSSClient) GetSignUrl(bucket string, key string, expires time.Duration) (result *oss.PresignResult, err error) {
|
||||
// BuildPutSignUrl 获取上传签名
|
||||
func (c *ALiYunOSSClient) BuildPutSignUrl(bucket string, key string, expires time.Duration) (result *oss.PresignResult, err error) {
|
||||
// 生成PutObject的预签名URL
|
||||
result, err = c.ossClient.Presign(
|
||||
context.Background(),
|
||||
&oss.PutObjectRequest{
|
||||
Bucket: oss.Ptr(bucket),
|
||||
Key: oss.Ptr(key),
|
||||
//ContentType: oss.Ptr("application/octet-stream"),
|
||||
},
|
||||
oss.PresignExpires(expires),
|
||||
)
|
||||
@@ -52,6 +53,33 @@ func (c *ALiYunOSSClient) GetSignUrl(bucket string, key string, expires time.Dur
|
||||
return
|
||||
}
|
||||
|
||||
// BuildPutSignUrlByPutObjectRequest 根据PutObjectRequest生成PutObject的预签名URL
|
||||
func (c *ALiYunOSSClient) BuildPutSignUrlByPutObjectRequest(req *oss.PutObjectRequest, expires time.Duration) (result *oss.PresignResult, err error) {
|
||||
// 生成PutObject的预签名URL
|
||||
result, err = c.ossClient.Presign(
|
||||
context.Background(),
|
||||
req,
|
||||
oss.PresignExpires(expires),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// BuildSignUrlByGetObjectRequest 根据GetObjectRequest生成GetObject的预签名URL
|
||||
func (c *ALiYunOSSClient) BuildSignUrlByGetObjectRequest(req *oss.GetObjectRequest) (result *oss.PresignResult, err error) {
|
||||
// 生成PutObject的预签名URL
|
||||
result, err = c.ossClient.Presign(
|
||||
context.Background(),
|
||||
req,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PutForLocalFile 上传本地文件
|
||||
func (c *ALiYunOSSClient) PutForLocalFile(bucket, key, path string) (result *oss.PutObjectResult, err error) {
|
||||
// 创建上传对象的请求
|
||||
@@ -145,3 +173,18 @@ func (c *ALiYunOSSClient) GetObjectToImage(bucket string, key string) (img image
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// DelObject 删除对象
|
||||
func (c *ALiYunOSSClient) DelObject(bucket string, key string) (result *oss.DeleteObjectResult, err error) {
|
||||
// 创建删除对象的请求
|
||||
request := &oss.DeleteObjectRequest{
|
||||
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||
Key: oss.Ptr(key), // 对象名称
|
||||
}
|
||||
// 执行删除对象的操作并处理结果
|
||||
result, err = c.ossClient.DeleteObject(context.TODO(), request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package oss_tool
|
||||
|
||||
import (
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -24,12 +25,28 @@ func TestALiYunOSSClient_NewAliYunOSS(t *testing.T) {
|
||||
t.Log(client.ossClient)
|
||||
}
|
||||
|
||||
func TestALiYunOSSClient_GetSignUrl(t *testing.T) {
|
||||
func TestALiYunOSSClient_BuildPutSignUrl(t *testing.T) {
|
||||
err := client.NewAliYunOSS()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
sign, err := client.GetSignUrl("", "test/upload/bizhi1.jpg", 0)
|
||||
sign, err := client.BuildPutSignUrl("", "test/upload/bizhi1.jpg", 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(sign)
|
||||
}
|
||||
|
||||
func TestALiYunOSSClient_BuildPutSignUrlByPutObjectRequest(t *testing.T) {
|
||||
err := client.NewAliYunOSS()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
req := &oss.PutObjectRequest{}
|
||||
req.Bucket = oss.Ptr("")
|
||||
req.Key = oss.Ptr("test/upload/bizhi2.jpg")
|
||||
req.ContentType = oss.Ptr("application/octet-stream")
|
||||
sign, err := client.BuildPutSignUrlByPutObjectRequest(req, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -41,7 +58,22 @@ func TestALiYunOSSClient_PutForLocalFile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
result, err := client.PutForLocalFile("", "test/upload/bizhi2.jpg", "C:\\Users\\Administrator\\Desktop\\壁纸1.jpg")
|
||||
result, err := client.PutForLocalFile("ssgfdown", "test/upload/bizhi2.jpg", "")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(result)
|
||||
}
|
||||
|
||||
func TestALiYunOSSClient_BuildSignUrlByGetObjectRequest(t *testing.T) {
|
||||
err := client.NewAliYunOSS()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
req := &oss.GetObjectRequest{}
|
||||
req.Bucket = oss.Ptr("ssgfdown")
|
||||
req.Key = oss.Ptr("test/upload/bizhi2.jpg")
|
||||
result, err := client.BuildSignUrlByGetObjectRequest(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -73,3 +105,16 @@ func TestALiYunOSSClient_GetObjectToImage(t *testing.T) {
|
||||
t.Log(img)
|
||||
}
|
||||
}
|
||||
|
||||
func TestALiYunOSSClient_DelObject(t *testing.T) {
|
||||
err := client.NewAliYunOSS()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
res, err := client.DelObject("", "test/upload/bizhi2.jpg")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Log(res)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user