From 897a719154a2ce5a053e00b729f3299ba5af5825 Mon Sep 17 00:00:00 2001 From: yuxh Date: Wed, 17 Jul 2024 14:50:29 +0800 Subject: [PATCH] =?UTF-8?q?oss=E5=8C=85=E6=96=B0=E5=A2=9E=E5=88=86?= =?UTF-8?q?=E7=89=87=E4=BB=BB=E5=8A=A1=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20&=20=E8=8E=B7=E5=8F=96APK=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/oss/ApkVersion_get.go | 36 +++++++++++++++++++++ services/oss/client.go | 19 +++++++++++ services/oss/client_test.go | 34 ++++++++++++++++++++ services/oss/multipart_init.go | 58 ++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 services/oss/ApkVersion_get.go create mode 100644 services/oss/multipart_init.go diff --git a/services/oss/ApkVersion_get.go b/services/oss/ApkVersion_get.go new file mode 100644 index 0000000..47ed5d1 --- /dev/null +++ b/services/oss/ApkVersion_get.go @@ -0,0 +1,36 @@ +package oss + +import ( + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" +) + +type GetApkVersionRequest struct { + *requests.JsonRequest + Filepath string `position:"Body" field:"filepath"` +} + +type GetApkVersionResponse struct { + *responses.BaseResponse + Code int `json:"code"` + Msg string `json:"msg"` + Data struct { + VersionCode string `json:"versionCode"` + VersionName string `json:"versionName"` + } `json:"data"` +} + +func CreateGetApkVersionRequest() (req *GetApkVersionRequest) { + req = &GetApkVersionRequest{ + JsonRequest: &requests.JsonRequest{}, + } + req.InitWithApiInfo(HOST, VERSION, "/api/apk/version") + req.Method = requests.POST + return +} + +func CreateGetApkVersionResponse() *GetApkVersionResponse { + return &GetApkVersionResponse{ + BaseResponse: &responses.BaseResponse{}, + } +} diff --git a/services/oss/client.go b/services/oss/client.go index f8f7c0b..e0e93d1 100644 --- a/services/oss/client.go +++ b/services/oss/client.go @@ -3,6 +3,8 @@ package oss import ( "errors" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" ) @@ -24,6 +26,10 @@ func NewClient() (client *Client, err error) { return } +func NewClientWithSts() { + +} + func (c *Client) PutOss(req *PutOssRequest) (resp *PutOssResponse, err error) { if req.BucketName == "" { err = errors.New("bucket name is empty") @@ -44,3 +50,16 @@ func (c *Client) DelOss(req *DelOssRequest) (resp *DelOssResponse, err error) { err = c.DoAction(req, resp) return } + +func (c *Client) InitMultipartUpload(req *InitMultipartUploadRequest) (resp *InitMultipartUploadResponse, err error) { + resp = CreateInitMultipartUploadResponse() + err = c.DoAction(req, resp) + return +} + +func (c *Client) GetApkVersion(req *GetApkVersionRequest) (resp *GetApkVersionResponse, err error) { + resp = CreateGetApkVersionResponse() + sign, err := auth.NewSignerWithCredential(credentials.NewStsTokenCredential("", "ccc", ""), nil) + err = c.DoActionWithSigner(req, resp, sign) + return +} diff --git a/services/oss/client_test.go b/services/oss/client_test.go index 0f77109..612e58e 100644 --- a/services/oss/client_test.go +++ b/services/oss/client_test.go @@ -39,3 +39,37 @@ func TestUpload_Del(t *testing.T) { } t.Log(resp.Code) } + +func TestCreateInitMultipartUpload(t *testing.T) { + req := CreateInitMultipartUploadRequest() + req.Filepath = "test.jpg" + req.TargetType = "oss" + req.TargetName = "image" + client, err := NewClient() + if err != nil { + t.Error(err) + return + } + resp, err := client.InitMultipartUpload(req) + if err != nil { + t.Error(err) + return + } + t.Log(resp) +} + +func TestClient_GetApkVersion(t *testing.T) { + req := CreateGetApkVersionRequest() + req.Filepath = "36c55c4c3a2f4c79e3917b989d580496.zip" + client, err := NewClient() + if err != nil { + t.Error(err) + return + } + resp, err := client.GetApkVersion(req) + if err != nil { + t.Error(err) + return + } + t.Log(resp.Data.VersionCode, resp.Data.VersionName) +} diff --git a/services/oss/multipart_init.go b/services/oss/multipart_init.go new file mode 100644 index 0000000..4e7642c --- /dev/null +++ b/services/oss/multipart_init.go @@ -0,0 +1,58 @@ +package oss + +import ( + "crypto/md5" + "encoding/hex" + "fmt" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" + "time" +) + +const ( + multipartUploadKeys = "b4471b940d98" +) + +type InitMultipartUploadRequest struct { + *requests.JsonRequest + Filepath string `position:"Body" field:"filepath"` + TargetType string `position:"Body" field:"target_type"` + TargetName string `position:"Body" field:"target_name"` + FileHash string `position:"Body" field:"file_hash"` +} + +type InitMultipartUploadResponse struct { + *responses.BaseResponse + Code int `json:"code"` + Msg string `json:"msg"` + Data struct { + FileUploaded bool `json:"file_uploaded"` + UploadId string `json:"upload_id,omitempty"` + Links []Link `json:"links,omitempty"` + } `json:"data"` +} + +type Link struct { + Href string `json:"href"` + Rel string `json:"rel"` + Method string `json:"method"` +} + +func CreateInitMultipartUploadRequest() (req *InitMultipartUploadRequest) { + ts := fmt.Sprintf("%d", time.Now().Unix()) + m := md5.Sum([]byte(ts + multipartUploadKeys)) + sign := hex.EncodeToString(m[:]) + req = &InitMultipartUploadRequest{ + JsonRequest: &requests.JsonRequest{}, + } + + req.InitWithApiInfo(HOST, VERSION, "/api/upload/multipart/init?ts="+ts+"&sign="+sign) + return +} + +func CreateInitMultipartUploadResponse() (resp *InitMultipartUploadResponse) { + resp = &InitMultipartUploadResponse{ + BaseResponse: &responses.BaseResponse{}, + } + return resp +}