Skip to content

Commit

Permalink
feat: oss
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Jun 18, 2021
1 parent eee740e commit 60f8603
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 24 additions & 5 deletions collector/server/controller/profile/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"fmt"
"net/http"
"time"

"github.com/aliyun/aliyun-tablestore-go-sdk/v5/tablestore"
"github.com/gin-gonic/gin"
Expand All @@ -25,6 +26,8 @@ type ReceiveProfileReq struct {
CreateTime int64 `json:"create_time"`
}

var cn = time.FixedZone("GMT", 8*3600)

func ReceiveProfile(c *gin.Context) {
logger := middleware.Env(c).Logger

Expand Down Expand Up @@ -70,10 +73,9 @@ func ReceiveProfile(c *gin.Context) {
return
}

err = bucket.PutObject(
UploadPath(oss.PathPrefix, req.Service, req.ProfileType, profileID),
buf,
)
objectName := UploadPath(oss.PathPrefix, req.Service, req.ProfileType, profileID)

err = bucket.PutObject(objectName, buf)
if err != nil {
logger().WithRequestId(c).Info("fail to upload",
zap.String("service", req.Service),
Expand All @@ -84,6 +86,17 @@ func ReceiveProfile(c *gin.Context) {
return
}

_, err = bucket.GetObjectDetailedMeta(objectName)
if err != nil {
logger().WithRequestId(c).Info("fail to query object",
zap.String("service", req.Service),
zap.String("service_version", req.ServiceVersion),
zap.String("ip", req.IP),
zap.Error(err))
c.AbortWithStatus(http.StatusInternalServerError)
return
}

tb := middleware.Env(c).TablestoreClient()

putRowRequest := new(tablestore.PutRowRequest)
Expand All @@ -100,6 +113,7 @@ func ReceiveProfile(c *gin.Context) {
putRowChange.AddColumn("profile_type", req.ProfileType)
putRowChange.AddColumn("send_time", req.SendTime)
putRowChange.AddColumn("create_time", req.CreateTime)
putRowChange.AddColumn("object_name", objectName)
putRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_NOT_EXIST)
putRowRequest.PutRowChange = putRowChange
_, err = tb.Client.PutRow(putRowRequest)
Expand All @@ -115,5 +129,10 @@ func ReceiveProfile(c *gin.Context) {
}

func UploadPath(pathPrefix, service, profileType, fileName string) string {
return fmt.Sprintf("%s/%s/%s/%s", pathPrefix, service, profileType, fileName)
return fmt.Sprintf("%s/%s/%s/%s/%s",
pathPrefix,
time.Now().In(cn).Format("2006-01-02"),
service,
profileType,
fileName)
}
7 changes: 7 additions & 0 deletions collector/server/controller/profile/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package profile

import "testing"

func TestUploadPath(t *testing.T) {
t.Logf(UploadPath("abc", "bcf", "cpu", "efg"))
}

0 comments on commit 60f8603

Please sign in to comment.