You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

obs.go 9.0 kB

4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "io"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/obs"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/unknwon/com"
  14. )
  15. type FileInfo struct {
  16. FileName string `json:"FileName"`
  17. ModTime string `json:"ModTime"`
  18. IsDir bool `json:"IsDir"`
  19. Size int64 `json:"Size"`
  20. ParenDir string `json:"ParenDir"`
  21. UUID string `json:"UUID"`
  22. }
  23. //check if has the object
  24. //todo:修改查询方式
  25. func ObsHasObject(path string) (bool, error) {
  26. hasObject := false
  27. output, err := ObsCli.ListObjects(&obs.ListObjectsInput{Bucket: setting.Bucket})
  28. if err != nil {
  29. log.Error("ListObjects failed:%v", err)
  30. return hasObject, err
  31. }
  32. for _, obj := range output.Contents {
  33. //obj.Key:attachment/0/1/019fd24e-4ef7-41cc-9f85-4a7b8504d958
  34. if path == obj.Key {
  35. hasObject = true
  36. break
  37. }
  38. }
  39. return hasObject, nil
  40. }
  41. func GetObsPartInfos(uuid string, uploadID string) (string, error) {
  42. key := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  43. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  44. Bucket: setting.Bucket,
  45. Key: key,
  46. UploadId: uploadID,
  47. })
  48. if err != nil {
  49. log.Error("ListParts failed:", err.Error())
  50. return "", err
  51. }
  52. var chunks string
  53. for _, partInfo := range output.Parts {
  54. chunks += strconv.Itoa(partInfo.PartNumber) + "-" + partInfo.ETag + ","
  55. }
  56. return chunks, nil
  57. }
  58. func NewObsMultiPartUpload(uuid, fileName string) (string, error) {
  59. input := &obs.InitiateMultipartUploadInput{}
  60. input.Bucket = setting.Bucket
  61. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  62. output, err := ObsCli.InitiateMultipartUpload(input)
  63. if err != nil {
  64. log.Error("InitiateMultipartUpload failed:", err.Error())
  65. return "", err
  66. }
  67. return output.UploadId, nil
  68. }
  69. func CompleteObsMultiPartUpload(uuid, uploadID, fileName string) error {
  70. input := &obs.CompleteMultipartUploadInput{}
  71. input.Bucket = setting.Bucket
  72. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  73. input.UploadId = uploadID
  74. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  75. Bucket: setting.Bucket,
  76. Key: input.Key,
  77. UploadId: uploadID,
  78. })
  79. if err != nil {
  80. log.Error("ListParts failed:", err.Error())
  81. return err
  82. }
  83. for _, partInfo := range output.Parts {
  84. input.Parts = append(input.Parts, obs.Part{
  85. PartNumber: partInfo.PartNumber,
  86. ETag: partInfo.ETag,
  87. })
  88. }
  89. _, err = ObsCli.CompleteMultipartUpload(input)
  90. if err != nil {
  91. log.Error("CompleteMultipartUpload failed:", err.Error())
  92. return err
  93. }
  94. return nil
  95. }
  96. func ObsMultiPartUpload(uuid string, uploadId string, partNumber int, fileName string, putBody io.ReadCloser) error {
  97. input := &obs.UploadPartInput{}
  98. input.Bucket = setting.Bucket
  99. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  100. input.UploadId = uploadId
  101. input.PartNumber = partNumber
  102. input.Body = putBody
  103. output, err := ObsCli.UploadPart(input)
  104. if err == nil {
  105. log.Info("RequestId:%s\n", output.RequestId)
  106. log.Info("ETag:%s\n", output.ETag)
  107. return nil
  108. } else {
  109. if obsError, ok := err.(obs.ObsError); ok {
  110. log.Info(obsError.Code)
  111. log.Info(obsError.Message)
  112. return obsError
  113. } else {
  114. log.Error("error:", err.Error())
  115. return err
  116. }
  117. }
  118. }
  119. func ObsDownload(uuid string, fileName string) (io.ReadCloser, error) {
  120. input := &obs.GetObjectInput{}
  121. input.Bucket = setting.Bucket
  122. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  123. // input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  124. output, err := ObsCli.GetObject(input)
  125. if err == nil {
  126. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  127. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  128. return output.Body, nil
  129. } else if obsError, ok := err.(obs.ObsError); ok {
  130. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  131. return nil, obsError
  132. } else {
  133. return nil, err
  134. }
  135. }
  136. func ObsModelDownload(JobName string, fileName string) (io.ReadCloser, error) {
  137. input := &obs.GetObjectInput{}
  138. input.Bucket = setting.Bucket
  139. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, JobName, setting.OutPutPath, fileName), "/")
  140. // input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  141. output, err := ObsCli.GetObject(input)
  142. if err == nil {
  143. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  144. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  145. return output.Body, nil
  146. } else if obsError, ok := err.(obs.ObsError); ok {
  147. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  148. return nil, obsError
  149. } else {
  150. return nil, err
  151. }
  152. }
  153. func GetObsListObject(jobName, parentDir string) ([]FileInfo, error) {
  154. input := &obs.ListObjectsInput{}
  155. input.Bucket = setting.Bucket
  156. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir), "/")
  157. strPrefix := strings.Split(input.Prefix, "/")
  158. output, err := ObsCli.ListObjects(input)
  159. fileInfos := make([]FileInfo, 0)
  160. if err == nil {
  161. for _, val := range output.Contents {
  162. str1 := strings.Split(val.Key, "/")
  163. var isDir bool
  164. var fileName, nextParentDir string
  165. if strings.HasSuffix(val.Key, "/") {
  166. //dirs in next level dir
  167. if len(str1)-len(strPrefix) > 2 {
  168. continue
  169. }
  170. fileName = str1[len(str1)-2]
  171. isDir = true
  172. if parentDir == "" {
  173. nextParentDir = fileName
  174. } else {
  175. nextParentDir = parentDir + "/" + fileName
  176. }
  177. if fileName == strPrefix[len(strPrefix)-1] || (fileName+"/") == setting.OutPutPath {
  178. continue
  179. }
  180. } else {
  181. //files in next level dir
  182. if len(str1)-len(strPrefix) > 1 {
  183. continue
  184. }
  185. fileName = str1[len(str1)-1]
  186. isDir = false
  187. nextParentDir = parentDir
  188. }
  189. fileInfo := FileInfo{
  190. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  191. FileName: fileName,
  192. Size: val.Size,
  193. IsDir: isDir,
  194. ParenDir: nextParentDir,
  195. }
  196. fileInfos = append(fileInfos, fileInfo)
  197. }
  198. return fileInfos, err
  199. } else {
  200. if obsError, ok := err.(obs.ObsError); ok {
  201. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  202. }
  203. return nil, err
  204. }
  205. }
  206. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  207. input := &obs.CreateSignedUrlInput{}
  208. input.Bucket = setting.Bucket
  209. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  210. input.Expires = 60 * 60
  211. input.Method = obs.HttpMethodPut
  212. input.QueryParams = map[string]string{
  213. "partNumber": com.ToStr(partNumber, 10),
  214. "uploadId": uploadId,
  215. //"partSize": com.ToStr(partSize,10),
  216. }
  217. output, err := ObsCli.CreateSignedUrl(input)
  218. if err != nil {
  219. log.Error("CreateSignedUrl failed:", err.Error())
  220. return "", err
  221. }
  222. return output.SignedUrl, nil
  223. }
  224. func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
  225. input := &obs.CreateSignedUrlInput{}
  226. input.Bucket = setting.Bucket
  227. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/")
  228. input.Expires = 60 * 60
  229. input.Method = obs.HttpMethodGet
  230. reqParams := make(map[string]string)
  231. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  232. input.QueryParams = reqParams
  233. output, err := ObsCli.CreateSignedUrl(input)
  234. if err != nil {
  235. log.Error("CreateSignedUrl failed:", err.Error())
  236. return "", err
  237. }
  238. return output.SignedUrl, nil
  239. }
  240. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  241. input := &obs.CreateSignedUrlInput{}
  242. input.Method = obs.HttpMethodGet
  243. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  244. input.Bucket = setting.Bucket
  245. input.Expires = 60 * 60
  246. reqParams := make(map[string]string)
  247. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  248. input.QueryParams = reqParams
  249. output, err := ObsCli.CreateSignedUrl(input)
  250. if err != nil {
  251. log.Error("CreateSignedUrl failed:", err.Error())
  252. return "", err
  253. }
  254. return output.SignedUrl, nil
  255. }
  256. func ObsCreateObject(path string) error {
  257. input := &obs.PutObjectInput{}
  258. input.Bucket = setting.Bucket
  259. input.Key = path
  260. _, err := ObsCli.PutObject(input)
  261. if err != nil {
  262. log.Error("PutObject failed:", err.Error())
  263. return err
  264. }
  265. return nil
  266. }