7
0

新增oss包

This commit is contained in:
余 欣怀 2024-05-30 18:40:57 +08:00
parent 6ad1f1ac2e
commit afb120c6f4
9 changed files with 283 additions and 2 deletions

View File

@ -83,6 +83,10 @@ func (client *Client) InitClientConfig() (config *Config) {
}
func (client *Client) Init() (err error) {
return client.InitWithAccessKey("", "", "")
}
func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyFrom string) (err error) {
config := client.InitWithConfig()
credential := &credentials.BaseCredential{

View File

@ -0,0 +1,66 @@
package requests
import (
"bytes"
"encoding/json"
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"strings"
)
type JsonRequest struct {
*baseRequest
}
func (request *JsonRequest) init() {
request.baseRequest = defaultBaseRequest()
request.Method = POST
}
func (request *JsonRequest) BuildUrl() string {
var hostname string
if request.Domain.Func == nil {
hostname = request.Domain.Default
} else if hostname = request.Domain.Func(request.GetEnv()); hostname == "" {
hostname = request.Domain.Default
}
url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), hostname)
if len(request.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, request.Port)
}
return url + request.BuildQueries()
}
func (request *JsonRequest) GetStyle() string {
return RPC
}
func (request *JsonRequest) BuildQueries() string {
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/")
request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams)
return request.queries
}
func (request *JsonRequest) GetActionName() string {
return request.actionName
}
func (request *JsonRequest) InitWithApiInfo(domain Host, version, urlPath string) {
request.init()
request.SetDomain(domain)
request.version = version
request.actionName = urlPath
}
func (request *JsonRequest) GetBodyReader() io.Reader {
if request.FormParams != nil && len(request.FormParams) > 0 {
body, err := json.Marshal(request.FormParams)
if err == nil {
return bytes.NewReader(body)
}
}
return strings.NewReader("")
}

View File

@ -14,6 +14,7 @@ import (
const (
RPC = "RPC"
ROA = "ROA"
STREAM = "STREAM"
HTTP = "HTTP"
HTTPS = "HTTPS"

View File

@ -0,0 +1,55 @@
package requests
import (
"bytes"
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"strings"
)
type StreamRequest struct {
*baseRequest
}
func (s *StreamRequest) init() {
s.baseRequest = defaultBaseRequest()
s.baseRequest.AddHeaderParam("Content-Type", "application/form-data")
s.Method = POST
}
func (s *StreamRequest) GetStyle() string {
return STREAM
}
func (s *StreamRequest) InitWithApiInfo(domain Host, version, urlPath string) {
s.init()
s.SetDomain(domain)
s.version = version
s.actionName = urlPath
}
func (s *StreamRequest) BuildUrl() string {
var hostname string
if s.Domain.Func == nil {
hostname = s.Domain.Default
} else if hostname = s.Domain.Func(s.GetEnv()); hostname == "" {
hostname = s.Domain.Default
}
url := fmt.Sprintf("%s://%s", strings.ToLower(s.Scheme), hostname)
if len(s.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, s.Port)
}
return url + s.BuildQueries()
}
func (s *StreamRequest) BuildQueries() string {
path := strings.TrimLeft(strings.TrimSpace(s.GetActionName()), "/")
s.queries = "/" + path + "?" + utils.GetUrlFormedMap(s.QueryParams)
return s.queries
}
func (s *StreamRequest) GetBodyReader() io.Reader {
return bytes.NewReader(s.Content)
}

46
services/oss/client.go Normal file
View File

@ -0,0 +1,46 @@
package oss
import (
"errors"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
)
const (
VERSION = "2024-05-30"
)
var HOST = requests.Host{
Default: "oss.gaore.com",
}
type Client struct {
sdk.Client
}
func NewClient() (client *Client, err error) {
client = new(Client)
err = client.Init()
return
}
func (c *Client) PutOss(req *PutOssRequest) (resp *PutOssResponse, err error) {
if req.BucketName == "" {
err = errors.New("bucket name is empty")
return
}
if req.FileStream == nil {
err = errors.New("stream is empty")
return
}
req.SetContent(req.FileStream)
resp = CreatePutOssResponse()
err = c.DoAction(req, resp)
return
}
func (c *Client) DelOss(req *DelOssRequest) (resp *DelOssResponse, err error) {
resp = CreateDelOssResponse()
err = c.DoAction(req, resp)
return
}

View File

@ -0,0 +1,41 @@
package oss
import (
"os"
"testing"
)
func TestUpload_Put(t *testing.T) {
req := CreatePutOssRequest()
req.BucketName = "image"
file, err := os.ReadFile("test.jpg")
if err != nil {
t.Error(err)
return
}
req.FileStream = file
client, err := NewClient()
resp, err := client.PutOss(req)
if err != nil {
t.Error(err)
return
}
t.Log(resp.Data.Url)
}
func TestUpload_Del(t *testing.T) {
req := CreateDelOssRequest()
req.BucketName = "image"
req.MediaUrl = "https://image.89yoo.com/uploads/549/549e887460a72333c361661683023018.jpeg"
client, err := NewClient()
if err != nil {
t.Error(err)
return
}
resp, err := client.DelOss(req)
if err != nil {
t.Error(err)
return
}
t.Log(resp.Code)
}

BIN
services/oss/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 630 KiB

View File

@ -0,0 +1,31 @@
package oss
import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
)
type DelOssRequest struct {
*requests.JsonRequest
BucketName string `position:"Body" field:"bucket_name" default:"image"`
MediaUrl string `position:"Body" field:"url" default:"-"`
}
type DelOssResponse struct {
*responses.BaseResponse
}
func CreateDelOssRequest() (req *DelOssRequest) {
req = &DelOssRequest{
JsonRequest: &requests.JsonRequest{},
}
req.InitWithApiInfo(HOST, VERSION, "/api/upload/del")
req.Method = requests.POST
return
}
func CreateDelOssResponse() (resp *DelOssResponse) {
return &DelOssResponse{
BaseResponse: &responses.BaseResponse{},
}
}

View File

@ -0,0 +1,37 @@
package oss
import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
)
type PutOssRequest struct {
*requests.StreamRequest
BucketName string `position:"Query" field:"bucket_name" default:"image"`
FileStream []byte
}
type PutOssResponse struct {
*responses.BaseResponse
Code int `json:"code"`
Msg string `json:"msg"`
Data struct {
Url string `json:"url"`
}
}
func CreatePutOssRequest() (req *PutOssRequest) {
req = &PutOssRequest{
StreamRequest: &requests.StreamRequest{},
}
req.InitWithApiInfo(HOST, VERSION, "api/upload/put")
req.Method = requests.POST
return
}
func CreatePutOssResponse() (resp *PutOssResponse) {
return &PutOssResponse{
BaseResponse: &responses.BaseResponse{},
}
}