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 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. //todo:修改查询方式
  27. func ObsHasObject(path string) (bool, error) {
  28. hasObject := false
  29. output, err := ObsCli.ListObjects(&obs.ListObjectsInput{Bucket: setting.Bucket})
  30. if err != nil {
  31. log.Error("ListObjects failed:%v", err)
  32. return hasObject, err
  33. }
  34. for _, obj := range output.Contents {
  35. //obj.Key:attachment/0/1/019fd24e-4ef7-41cc-9f85-4a7b8504d958
  36. if path == obj.Key {
  37. hasObject = true
  38. break
  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 GetObsListObjectVersion(jobName, parentDir string, VersionOutputPath string) ([]FileInfo, error) {
  212. input := &obs.ListObjectsInput{}
  213. input.Bucket = setting.Bucket
  214. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, VersionOutputPath, parentDir), "/")
  215. strPrefix := strings.Split(input.Prefix, "/")
  216. output, err := ObsCli.ListObjects(input)
  217. fileInfos := make([]FileInfo, 0)
  218. if err == nil {
  219. for _, val := range output.Contents {
  220. str1 := strings.Split(val.Key, "/")
  221. var isDir bool
  222. var fileName, nextParentDir string
  223. if strings.HasSuffix(val.Key, "/") {
  224. //dirs in next level dir
  225. if len(str1)-len(strPrefix) > 2 {
  226. continue
  227. }
  228. fileName = str1[len(str1)-2]
  229. isDir = true
  230. if parentDir == "" {
  231. nextParentDir = fileName
  232. } else {
  233. nextParentDir = parentDir + "/" + fileName
  234. }
  235. if fileName == strPrefix[len(strPrefix)-1] || (fileName+"/") == setting.OutPutPath {
  236. continue
  237. }
  238. } else {
  239. //files in next level dir
  240. if len(str1)-len(strPrefix) > 1 {
  241. continue
  242. }
  243. fileName = str1[len(str1)-1]
  244. isDir = false
  245. nextParentDir = parentDir
  246. }
  247. fileInfo := FileInfo{
  248. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  249. FileName: fileName,
  250. Size: val.Size,
  251. IsDir: isDir,
  252. ParenDir: nextParentDir,
  253. }
  254. fileInfos = append(fileInfos, fileInfo)
  255. }
  256. sort.Slice(fileInfos, func(i, j int) bool {
  257. return fileInfos[i].ModTime > fileInfos[j].ModTime
  258. })
  259. return fileInfos, err
  260. } else {
  261. if obsError, ok := err.(obs.ObsError); ok {
  262. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  263. }
  264. return nil, err
  265. }
  266. }
  267. func GetVersionObsListObject(jobName, parentDir string) ([]FileInfo, error) {
  268. input := &obs.ListObjectsInput{}
  269. input.Bucket = setting.Bucket
  270. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir), "/")
  271. strPrefix := strings.Split(input.Prefix, "/")
  272. output, err := ObsCli.ListObjects(input)
  273. fileInfos := make([]FileInfo, 0)
  274. if err == nil {
  275. for _, val := range output.Contents {
  276. str1 := strings.Split(val.Key, "/")
  277. var isDir bool
  278. var fileName, nextParentDir string
  279. if strings.HasSuffix(val.Key, "/") {
  280. //dirs in next level dir
  281. if len(str1)-len(strPrefix) > 2 {
  282. continue
  283. }
  284. fileName = str1[len(str1)-2]
  285. isDir = true
  286. if parentDir == "" {
  287. nextParentDir = fileName
  288. } else {
  289. nextParentDir = parentDir + "/" + fileName
  290. }
  291. if fileName == strPrefix[len(strPrefix)-1] || (fileName+"/") == setting.OutPutPath {
  292. continue
  293. }
  294. } else {
  295. //files in next level dir
  296. if len(str1)-len(strPrefix) > 1 {
  297. continue
  298. }
  299. fileName = str1[len(str1)-1]
  300. isDir = false
  301. nextParentDir = parentDir
  302. }
  303. fileInfo := FileInfo{
  304. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  305. FileName: fileName,
  306. Size: val.Size,
  307. IsDir: isDir,
  308. ParenDir: nextParentDir,
  309. }
  310. fileInfos = append(fileInfos, fileInfo)
  311. }
  312. return fileInfos, err
  313. } else {
  314. if obsError, ok := err.(obs.ObsError); ok {
  315. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  316. }
  317. return nil, err
  318. }
  319. }
  320. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  321. input := &obs.CreateSignedUrlInput{}
  322. input.Bucket = setting.Bucket
  323. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  324. input.Expires = 60 * 60
  325. input.Method = obs.HttpMethodPut
  326. input.QueryParams = map[string]string{
  327. "partNumber": com.ToStr(partNumber, 10),
  328. "uploadId": uploadId,
  329. //"partSize": com.ToStr(partSize,10),
  330. }
  331. output, err := ObsCli.CreateSignedUrl(input)
  332. if err != nil {
  333. log.Error("CreateSignedUrl failed:", err.Error())
  334. return "", err
  335. }
  336. return output.SignedUrl, nil
  337. }
  338. func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
  339. input := &obs.CreateSignedUrlInput{}
  340. input.Bucket = setting.Bucket
  341. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/")
  342. input.Expires = 60 * 60
  343. input.Method = obs.HttpMethodGet
  344. reqParams := make(map[string]string)
  345. fileName = url.QueryEscape(fileName)
  346. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  347. input.QueryParams = reqParams
  348. output, err := ObsCli.CreateSignedUrl(input)
  349. if err != nil {
  350. log.Error("CreateSignedUrl failed:", err.Error())
  351. return "", err
  352. }
  353. return output.SignedUrl, nil
  354. }
  355. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  356. input := &obs.CreateSignedUrlInput{}
  357. input.Method = obs.HttpMethodGet
  358. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  359. input.Bucket = setting.Bucket
  360. input.Expires = 60 * 60
  361. reqParams := make(map[string]string)
  362. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  363. input.QueryParams = reqParams
  364. output, err := ObsCli.CreateSignedUrl(input)
  365. if err != nil {
  366. log.Error("CreateSignedUrl failed:", err.Error())
  367. return "", err
  368. }
  369. return output.SignedUrl, nil
  370. }
  371. func ObsCreateObject(path string) error {
  372. input := &obs.PutObjectInput{}
  373. input.Bucket = setting.Bucket
  374. input.Key = path
  375. _, err := ObsCli.PutObject(input)
  376. if err != nil {
  377. log.Error("PutObject failed:", err.Error())
  378. return err
  379. }
  380. return nil
  381. }