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