ssgf_utils/oss_tool/aliyun_oss_test.go
2025-07-16 15:54:18 +08:00

121 lines
2.5 KiB
Go

package oss_tool
import (
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"os"
"testing"
)
var (
accessKeyId = os.Getenv("OSS_ACCESS_KEY_ID")
accessKeySecret = os.Getenv("OSS_SECRET_ACCESS_KEY")
region = os.Getenv("OSS_REGION")
client = &ALiYunOSSClient{
AccessKeyID: accessKeyId,
AccessKeySecret: accessKeySecret,
Region: region,
}
)
func TestALiYunOSSClient_NewAliYunOSS(t *testing.T) {
err := client.NewAliYunOSS()
if err != nil {
t.Error(err)
}
t.Log(client.ossClient)
}
func TestALiYunOSSClient_BuildPutSignUrl(t *testing.T) {
err := client.NewAliYunOSS()
if err != nil {
t.Error(err)
}
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)
}
t.Log(sign)
}
func TestALiYunOSSClient_PutForLocalFile(t *testing.T) {
err := client.NewAliYunOSS()
if err != nil {
t.Error(err)
}
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)
}
t.Log(result)
}
func TestALiYunOSSClient_GetObjectToFile(t *testing.T) {
err := client.NewAliYunOSS()
if err != nil {
t.Error(err)
}
err = client.GetObjectToFile("", "test/upload/bizhi1.jpg", "D:/bizhi1.jpg")
if err != nil {
t.Error(err)
} else {
t.Log("成功")
}
}
func TestALiYunOSSClient_GetObjectToImage(t *testing.T) {
err := client.NewAliYunOSS()
if err != nil {
t.Error(err)
}
img, err := client.GetObjectToImage("", "test/upload/bizhi1.jpg")
if err != nil {
t.Error(err)
} else {
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)
}
}