package common import ( "github.com/ouxuanserver/osmanthuswine/src/helper" "context" "fmt" "hudongzhuanjia/controllers" "net/http" "net/url" "path" "time" "github.com/tencentyun/cos-go-sdk-v5" ) const upload_base_path = "html/annex" type AnnexCtl struct { controllers.AuthorCtl } //上传 func (t *AnnexCtl) Upload() { //计算文件得md5 uploadFile, err := t.Request.FILE.Open() u, _ := url.Parse("https://hudongzhuanjia-1251209212.cos.ap-guangzhou.myqcloud.com") b := &cos.BaseURL{BucketURL: u} client := cos.NewClient(b, &http.Client{ Transport: &cos.AuthorizationTransport{ SecretID: "AKIDo2B9WqpvkSKQPPpplVAdv65QdpDfMRIz", SecretKey: "fEcu35xdCbpU3azFLH2lU2f8UpAOQHZs", }, }) picName := fmt.Sprintf("hdzj/upload/%d%s%s", time.Now().UnixNano(), helper.CreateUUID(), path.Ext(t.Request.FILE.Filename)) _, err = client.Object.Put(context.Background(), picName, uploadFile, nil) t.CheckErrDisplayByError(err) t.DisplayByData(map[string]interface{}{ "url": fmt.Sprintf("%s/%s", u.String(), picName), }) }