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.9 kB

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