Browse Source

Merge pull request '下载云脑2模型' (#643) from download-cb2-model into V20211101

Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/643
Reviewed-by: liuzx <liuzx@pcl.ac.cn>
tags/v1.21.12.1
liuzx 3 years ago
parent
commit
51024fa4e0
5 changed files with 39 additions and 46 deletions
  1. +24
    -16
      modules/storage/obs.go
  2. +0
    -28
      routers/repo/cloudbrain.go
  3. +13
    -0
      routers/repo/modelarts.go
  4. +1
    -1
      templates/repo/modelarts/trainjob/models/dir_list.tmpl
  5. +1
    -1
      templates/repo/modelarts/trainjob/models/index.tmpl

+ 24
- 16
modules/storage/obs.go View File

@@ -181,17 +181,28 @@ func GetObsListObject(jobName, parentDir string) ([]FileInfo, error) {
output, err := ObsCli.ListObjects(input)
fileInfos := make([]FileInfo, 0)
if err == nil {
for index, val := range output.Contents {
log.Info("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n",
index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size)
for _, val := range output.Contents {
str1 := strings.Split(val.Key, "/")
fileName := str1[len(str1)-1]
log.Info("", fileName)
var isDir bool
var fileName,nextParentDir string
if strings.HasSuffix(val.Key, "/") {
fileName = str1[len(str1)-2]
isDir = true
nextParentDir = fileName
if fileName == parentDir || (fileName + "/") == setting.OutPutPath {
continue
}
} else {
fileName = str1[len(str1)-1]
isDir = false
}

fileInfo := FileInfo{
ModTime: val.LastModified.Format("2006-01-02 15:04:05"),
FileName: fileName,
Size: val.Size,
IsDir:false,
IsDir:isDir,
ParenDir: nextParentDir,
}
fileInfos = append(fileInfos, fileInfo)
}
@@ -227,20 +238,17 @@ func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, file
return output.SignedUrl, nil
}

func GetObsCreateSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {

func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
input := &obs.CreateSignedUrlInput{}
input.Bucket = setting.Bucket
input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/")
input.Expires = 60 * 60
input.Method = obs.HttpMethodPut

input.QueryParams = map[string]string{
"partNumber": com.ToStr(partNumber, 10),
"uploadId": uploadId,
//"partSize": com.ToStr(partSize,10),
}
input.Method = obs.HttpMethodGet

reqParams := make(map[string]string)
reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
input.QueryParams = reqParams
output, err := ObsCli.CreateSignedUrl(input)
if err != nil {
log.Error("CreateSignedUrl failed:", err.Error())


+ 0
- 28
routers/repo/cloudbrain.go View File

@@ -583,34 +583,6 @@ func CloudBrainDownloadModel(ctx *context.Context) {
http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
}

func TrainJobDownloadModel(ctx *context.Context) {
JobName := ctx.Query("JobName")
fileName := ctx.Query("file_name")

body, err := storage.ObsModelDownload(JobName, fileName)
if err != nil {
log.Info("download error.")
} else {
defer body.Close()
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+fileName)
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
p := make([]byte, 1024)
var readErr error
var readCount int
// 读取对象内容
for {
readCount, readErr = body.Read(p)
if readCount > 0 {
ctx.Resp.Write(p[:readCount])
//fmt.Printf("%s", p[:readCount])
}
if readErr != nil {
break
}
}
}
}

func GetRate(ctx *context.Context) {
var jobID = ctx.Params(":jobid")
job, err := models.GetCloudbrainByJobID(jobID)


+ 13
- 0
routers/repo/modelarts.go View File

@@ -1086,3 +1086,16 @@ func TrainJobShowModels(ctx *context.Context) {
ctx.Data["JobID"] = jobID
ctx.HTML(200, tplModelArtsTrainJobShowModels)
}

func TrainJobDownloadModel(ctx *context.Context) {
parentDir := ctx.Query("parentDir")
fileName := ctx.Query("fileName")
jobName := ctx.Query("jobName")
url, err := storage.GetObsCreateSignedUrl(jobName, parentDir, fileName)
if err != nil {
log.Error("GetObsCreateSignedUrl failed: %v", err.Error(), ctx.Data["msgID"])
ctx.ServerError("GetObsCreateSignedUrl", err)
return
}
http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
}

+ 1
- 1
templates/repo/modelarts/trainjob/models/dir_list.tmpl View File

@@ -6,7 +6,7 @@
<td class="name four wide">
<span class="truncate">
<span class="octicon octicon-file-directory"></span>
<a class="title" href="{{if .IsDir}}{{$.RepoLink}}/cloudbrain/{{$.JobID}}/models?parentDir={{.ParenDir}}{{else}}{{$.RepoLink}}/cloudbrain/{{$.JobID}}/download_model?parentDir={{.ParenDir}}&fileName={{.FileName}}&jobName={{$.task.JobName}}{{end}}">
<a class="title" href="{{if .IsDir}}{{$.RepoLink}}/modelarts/train-job/{{$.JobID}}/models?parentDir={{.ParenDir}}{{else}}{{$.RepoLink}}/modelarts/train-job/{{$.JobID}}/download_model?parentDir={{.ParenDir}}&fileName={{.FileName}}&jobName={{$.task.JobName}}{{end}}">
<span class="fitted">{{if .IsDir}} {{svg "octicon-file-directory" 16}}{{else}}{{svg "octicon-file" 16}}{{end}}</span> {{.FileName}}
</a>
</span>


+ 1
- 1
templates/repo/modelarts/trainjob/models/index.tmpl View File

@@ -16,7 +16,7 @@
<div class="row">
<div class="ui sixteen wide column">
<div class="dir list">
{{template "repo/cloudbrain/models/dir_list" .}}
{{template "repo/modelarts/trainjob/models/dir_list" .}}
</div>
</div>
</div>


Loading…
Cancel
Save