diff --git a/oss_tool/aliyun_oss.go b/oss_tool/aliyun_oss.go index 06e6b49..065a91d 100644 --- a/oss_tool/aliyun_oss.go +++ b/oss_tool/aliyun_oss.go @@ -145,3 +145,18 @@ func (c *ALiYunOSSClient) GetObjectToImage(bucket string, key string) (img image } return img, nil } + +// DelObject 删除对象 +func (c *ALiYunOSSClient) DelObject(bucket string, key string) (err error) { + // 创建删除对象的请求 + request := &oss.DeleteObjectRequest{ + Bucket: oss.Ptr(bucket), // 存储空间名称 + Key: oss.Ptr(key), // 对象名称 + } + // 执行删除对象的操作并处理结果 + _, err = c.ossClient.DeleteObject(context.TODO(), request) + if err != nil { + return err + } + return nil +} diff --git a/oss_tool/aliyun_oss_test.go b/oss_tool/aliyun_oss_test.go index b94302e..637c244 100644 --- a/oss_tool/aliyun_oss_test.go +++ b/oss_tool/aliyun_oss_test.go @@ -73,3 +73,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) + } + err = client.DelObject("", "test/upload/bizhi2.jpg") + if err != nil { + t.Error(err) + } else { + t.Log("成功") + } +}