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 8.8 kB

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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. "github.com/unknwon/com"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/obs"
  13. "code.gitea.io/gitea/modules/setting"
  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. fileName = str1[len(str1)-2]
  167. isDir = true
  168. nextParentDir = fileName
  169. if fileName == parentDir || (fileName + "/") == setting.OutPutPath {
  170. continue
  171. }
  172. } else {
  173. //files in next level dir
  174. if len(str1) - len(strPrefix) > 1 {
  175. continue
  176. }
  177. fileName = str1[len(str1)-1]
  178. isDir = false
  179. }
  180. fileInfo := FileInfo{
  181. ModTime: val.LastModified.Format("2006-01-02 15:04:05"),
  182. FileName: fileName,
  183. Size: val.Size,
  184. IsDir:isDir,
  185. ParenDir: nextParentDir,
  186. }
  187. fileInfos = append(fileInfos, fileInfo)
  188. }
  189. return fileInfos, err
  190. } else {
  191. if obsError, ok := err.(obs.ObsError); ok {
  192. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  193. }
  194. return nil, err
  195. }
  196. }
  197. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  198. input := &obs.CreateSignedUrlInput{}
  199. input.Bucket = setting.Bucket
  200. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  201. input.Expires = 60 * 60
  202. input.Method = obs.HttpMethodPut
  203. input.QueryParams = map[string]string{
  204. "partNumber": com.ToStr(partNumber, 10),
  205. "uploadId": uploadId,
  206. //"partSize": com.ToStr(partSize,10),
  207. }
  208. output, err := ObsCli.CreateSignedUrl(input)
  209. if err != nil {
  210. log.Error("CreateSignedUrl failed:", err.Error())
  211. return "", err
  212. }
  213. return output.SignedUrl, nil
  214. }
  215. func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
  216. input := &obs.CreateSignedUrlInput{}
  217. input.Bucket = setting.Bucket
  218. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/")
  219. input.Expires = 60 * 60
  220. input.Method = obs.HttpMethodGet
  221. reqParams := make(map[string]string)
  222. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  223. input.QueryParams = reqParams
  224. output, err := ObsCli.CreateSignedUrl(input)
  225. if err != nil {
  226. log.Error("CreateSignedUrl failed:", err.Error())
  227. return "", err
  228. }
  229. return output.SignedUrl, nil
  230. }
  231. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  232. input := &obs.CreateSignedUrlInput{}
  233. input.Method = obs.HttpMethodGet
  234. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  235. input.Bucket = setting.Bucket
  236. input.Expires = 60 * 60
  237. reqParams := make(map[string]string)
  238. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  239. input.QueryParams = reqParams
  240. output, err := ObsCli.CreateSignedUrl(input)
  241. if err != nil {
  242. log.Error("CreateSignedUrl failed:", err.Error())
  243. return "", err
  244. }
  245. return output.SignedUrl, nil
  246. }
  247. func ObsCreateObject(path string) error {
  248. input := &obs.PutObjectInput{}
  249. input.Bucket = setting.Bucket
  250. input.Key = path
  251. _, err := ObsCli.PutObject(input)
  252. if err != nil {
  253. log.Error("PutObject failed:", err.Error())
  254. return err
  255. }
  256. return nil
  257. }