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 16 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. "errors"
  7. "io"
  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. //delete all file under the dir path
  121. func ObsRemoveObject(bucket string, path string) error {
  122. log.Info("Bucket=" + bucket + " path=" + path)
  123. if len(path) == 0 {
  124. return errors.New("path canot be null.")
  125. }
  126. input := &obs.ListObjectsInput{}
  127. input.Bucket = bucket
  128. // 设置每页100个对象
  129. input.MaxKeys = 100
  130. input.Prefix = path
  131. index := 1
  132. log.Info("prefix=" + input.Prefix)
  133. for {
  134. output, err := ObsCli.ListObjects(input)
  135. if err == nil {
  136. log.Info("Page:%d\n", index)
  137. index++
  138. for _, val := range output.Contents {
  139. log.Info("delete obs file:" + val.Key)
  140. delObj := &obs.DeleteObjectInput{}
  141. delObj.Bucket = setting.Bucket
  142. delObj.Key = val.Key
  143. ObsCli.DeleteObject(delObj)
  144. }
  145. if output.IsTruncated {
  146. input.Marker = output.NextMarker
  147. } else {
  148. break
  149. }
  150. } else {
  151. if obsError, ok := err.(obs.ObsError); ok {
  152. log.Info("Code:%s\n", obsError.Code)
  153. log.Info("Message:%s\n", obsError.Message)
  154. }
  155. return err
  156. }
  157. }
  158. return nil
  159. }
  160. func ObsDownloadAFile(bucket string, key string) (io.ReadCloser, error) {
  161. input := &obs.GetObjectInput{}
  162. input.Bucket = bucket
  163. input.Key = key
  164. output, err := ObsCli.GetObject(input)
  165. if err == nil {
  166. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  167. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  168. return output.Body, nil
  169. } else if obsError, ok := err.(obs.ObsError); ok {
  170. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  171. return nil, obsError
  172. } else {
  173. return nil, err
  174. }
  175. }
  176. func ObsDownload(uuid string, fileName string) (io.ReadCloser, error) {
  177. return ObsDownloadAFile(setting.Bucket, strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/"))
  178. }
  179. func ObsModelDownload(JobName string, fileName string) (io.ReadCloser, error) {
  180. input := &obs.GetObjectInput{}
  181. input.Bucket = setting.Bucket
  182. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, JobName, setting.OutPutPath, fileName), "/")
  183. // input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  184. output, err := ObsCli.GetObject(input)
  185. if err == nil {
  186. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  187. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  188. return output.Body, nil
  189. } else if obsError, ok := err.(obs.ObsError); ok {
  190. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  191. return nil, obsError
  192. } else {
  193. return nil, err
  194. }
  195. }
  196. func ObsCopyManyFile(srcBucket string, srcPath string, destBucket string, destPath string) (int64, error) {
  197. input := &obs.ListObjectsInput{}
  198. input.Bucket = srcBucket
  199. // 设置每页100个对象
  200. input.MaxKeys = 100
  201. input.Prefix = srcPath
  202. index := 1
  203. length := len(srcPath)
  204. var fileTotalSize int64
  205. log.Info("prefix=" + input.Prefix)
  206. for {
  207. output, err := ObsCli.ListObjects(input)
  208. if err == nil {
  209. log.Info("Page:%d\n", index)
  210. index++
  211. for _, val := range output.Contents {
  212. destKey := destPath + val.Key[length:]
  213. obsCopyFile(srcBucket, val.Key, destBucket, destKey)
  214. fileTotalSize += val.Size
  215. }
  216. if output.IsTruncated {
  217. input.Marker = output.NextMarker
  218. } else {
  219. break
  220. }
  221. } else {
  222. if obsError, ok := err.(obs.ObsError); ok {
  223. log.Info("Code:%s\n", obsError.Code)
  224. log.Info("Message:%s\n", obsError.Message)
  225. }
  226. return 0, err
  227. }
  228. }
  229. return fileTotalSize, nil
  230. }
  231. func obsCopyFile(srcBucket string, srcKeyName string, destBucket string, destKeyName string) error {
  232. input := &obs.CopyObjectInput{}
  233. input.Bucket = destBucket
  234. input.Key = destKeyName
  235. input.CopySourceBucket = srcBucket
  236. input.CopySourceKey = srcKeyName
  237. _, err := ObsCli.CopyObject(input)
  238. if err == nil {
  239. log.Info("copy success,destBuckName:%s, destkeyname:%s", destBucket, destKeyName)
  240. } else {
  241. log.Info("copy failed,,destBuckName:%s, destkeyname:%s", destBucket, destKeyName)
  242. if obsError, ok := err.(obs.ObsError); ok {
  243. log.Info(obsError.Code)
  244. log.Info(obsError.Message)
  245. }
  246. return err
  247. }
  248. return nil
  249. }
  250. func GetOneLevelAllObjectUnderDir(bucket string, prefixRootPath string, relativePath string) ([]FileInfo, error) {
  251. input := &obs.ListObjectsInput{}
  252. input.Bucket = bucket
  253. input.Prefix = prefixRootPath + relativePath
  254. output, err := ObsCli.ListObjects(input)
  255. fileInfos := make([]FileInfo, 0)
  256. prefixLen := len(input.Prefix)
  257. if err == nil {
  258. for _, val := range output.Contents {
  259. var isDir bool
  260. var fileName string
  261. if val.Key == input.Prefix {
  262. continue
  263. }
  264. if strings.Contains(val.Key[prefixLen:len(val.Key)-1], "/") {
  265. continue
  266. }
  267. if strings.HasSuffix(val.Key, "/") {
  268. isDir = true
  269. fileName = val.Key[prefixLen : len(val.Key)-1]
  270. relativePath += val.Key[prefixLen:]
  271. } else {
  272. isDir = false
  273. fileName = val.Key[prefixLen:]
  274. }
  275. fileInfo := FileInfo{
  276. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  277. FileName: fileName,
  278. Size: val.Size,
  279. IsDir: isDir,
  280. ParenDir: relativePath,
  281. }
  282. fileInfos = append(fileInfos, fileInfo)
  283. }
  284. return fileInfos, err
  285. } else {
  286. if obsError, ok := err.(obs.ObsError); ok {
  287. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  288. }
  289. return nil, err
  290. }
  291. }
  292. func GetAllObjectByBucketAndPrefix(bucket string, prefix string) ([]FileInfo, error) {
  293. input := &obs.ListObjectsInput{}
  294. input.Bucket = bucket
  295. // 设置每页100个对象
  296. input.MaxKeys = 100
  297. input.Prefix = prefix
  298. index := 1
  299. fileInfos := make([]FileInfo, 0)
  300. prefixLen := len(prefix)
  301. log.Info("prefix=" + input.Prefix)
  302. for {
  303. output, err := ObsCli.ListObjects(input)
  304. if err == nil {
  305. log.Info("Page:%d\n", index)
  306. index++
  307. for _, val := range output.Contents {
  308. var isDir bool
  309. if prefixLen == len(val.Key) {
  310. continue
  311. }
  312. if strings.HasSuffix(val.Key, "/") {
  313. isDir = true
  314. } else {
  315. isDir = false
  316. }
  317. fileInfo := FileInfo{
  318. ModTime: val.LastModified.Format("2006-01-02 15:04:05"),
  319. FileName: val.Key[prefixLen:],
  320. Size: val.Size,
  321. IsDir: isDir,
  322. ParenDir: "",
  323. }
  324. fileInfos = append(fileInfos, fileInfo)
  325. }
  326. if output.IsTruncated {
  327. input.Marker = output.NextMarker
  328. } else {
  329. break
  330. }
  331. } else {
  332. if obsError, ok := err.(obs.ObsError); ok {
  333. log.Info("Code:%s\n", obsError.Code)
  334. log.Info("Message:%s\n", obsError.Message)
  335. }
  336. return nil, err
  337. }
  338. }
  339. return fileInfos, nil
  340. }
  341. func GetObsListObject(jobName, parentDir string) ([]FileInfo, error) {
  342. input := &obs.ListObjectsInput{}
  343. input.Bucket = setting.Bucket
  344. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir), "/")
  345. strPrefix := strings.Split(input.Prefix, "/")
  346. output, err := ObsCli.ListObjects(input)
  347. fileInfos := make([]FileInfo, 0)
  348. if err == nil {
  349. for _, val := range output.Contents {
  350. str1 := strings.Split(val.Key, "/")
  351. var isDir bool
  352. var fileName, nextParentDir string
  353. if strings.HasSuffix(val.Key, "/") {
  354. //dirs in next level dir
  355. if len(str1)-len(strPrefix) > 2 {
  356. continue
  357. }
  358. fileName = str1[len(str1)-2]
  359. isDir = true
  360. if parentDir == "" {
  361. nextParentDir = fileName
  362. } else {
  363. nextParentDir = parentDir + "/" + fileName
  364. }
  365. if fileName == strPrefix[len(strPrefix)-1] || (fileName+"/") == setting.OutPutPath {
  366. continue
  367. }
  368. } else {
  369. //files in next level dir
  370. if len(str1)-len(strPrefix) > 1 {
  371. continue
  372. }
  373. fileName = str1[len(str1)-1]
  374. isDir = false
  375. nextParentDir = parentDir
  376. }
  377. fileInfo := FileInfo{
  378. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  379. FileName: fileName,
  380. Size: val.Size,
  381. IsDir: isDir,
  382. ParenDir: nextParentDir,
  383. }
  384. fileInfos = append(fileInfos, fileInfo)
  385. }
  386. return fileInfos, err
  387. } else {
  388. if obsError, ok := err.(obs.ObsError); ok {
  389. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  390. }
  391. return nil, err
  392. }
  393. }
  394. func GetVersionObsListObject(jobName, parentDir string) ([]FileInfo, error) {
  395. input := &obs.ListObjectsInput{}
  396. input.Bucket = setting.Bucket
  397. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir), "/")
  398. strPrefix := strings.Split(input.Prefix, "/")
  399. output, err := ObsCli.ListObjects(input)
  400. fileInfos := make([]FileInfo, 0)
  401. if err == nil {
  402. for _, val := range output.Contents {
  403. str1 := strings.Split(val.Key, "/")
  404. var isDir bool
  405. var fileName, nextParentDir string
  406. if strings.HasSuffix(val.Key, "/") {
  407. //dirs in next level dir
  408. if len(str1)-len(strPrefix) > 2 {
  409. continue
  410. }
  411. fileName = str1[len(str1)-2]
  412. isDir = true
  413. if parentDir == "" {
  414. nextParentDir = fileName
  415. } else {
  416. nextParentDir = parentDir + "/" + fileName
  417. }
  418. if fileName == strPrefix[len(strPrefix)-1] || (fileName+"/") == setting.OutPutPath {
  419. continue
  420. }
  421. } else {
  422. //files in next level dir
  423. if len(str1)-len(strPrefix) > 1 {
  424. continue
  425. }
  426. fileName = str1[len(str1)-1]
  427. isDir = false
  428. nextParentDir = parentDir
  429. }
  430. fileInfo := FileInfo{
  431. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  432. FileName: fileName,
  433. Size: val.Size,
  434. IsDir: isDir,
  435. ParenDir: nextParentDir,
  436. }
  437. fileInfos = append(fileInfos, fileInfo)
  438. }
  439. return fileInfos, err
  440. } else {
  441. if obsError, ok := err.(obs.ObsError); ok {
  442. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  443. }
  444. return nil, err
  445. }
  446. }
  447. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  448. input := &obs.CreateSignedUrlInput{}
  449. input.Bucket = setting.Bucket
  450. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  451. input.Expires = 60 * 60
  452. input.Method = obs.HttpMethodPut
  453. input.QueryParams = map[string]string{
  454. "partNumber": com.ToStr(partNumber, 10),
  455. "uploadId": uploadId,
  456. //"partSize": com.ToStr(partSize,10),
  457. }
  458. output, err := ObsCli.CreateSignedUrl(input)
  459. if err != nil {
  460. log.Error("CreateSignedUrl failed:", err.Error())
  461. return "", err
  462. }
  463. return output.SignedUrl, nil
  464. }
  465. func GetObsCreateSignedUrlByBucketAndKey(bucket, key string) (string, error) {
  466. input := &obs.CreateSignedUrlInput{}
  467. input.Bucket = bucket
  468. input.Key = key
  469. input.Expires = 60 * 60
  470. input.Method = obs.HttpMethodGet
  471. comma := strings.LastIndex(key, "/")
  472. filename := key
  473. if comma != -1 {
  474. filename = key[comma+1:]
  475. }
  476. reqParams := make(map[string]string)
  477. reqParams["response-content-disposition"] = "attachment; filename=\"" + filename + "\""
  478. input.QueryParams = reqParams
  479. output, err := ObsCli.CreateSignedUrl(input)
  480. if err != nil {
  481. log.Error("CreateSignedUrl failed:", err.Error())
  482. return "", err
  483. }
  484. return output.SignedUrl, nil
  485. }
  486. func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
  487. // input := &obs.CreateSignedUrlInput{}
  488. // input.Bucket = setting.Bucket
  489. // input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/")
  490. return GetObsCreateSignedUrlByBucketAndKey(setting.Bucket, strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/"))
  491. // input.Expires = 60 * 60
  492. // input.Method = obs.HttpMethodGet
  493. // reqParams := make(map[string]string)
  494. // reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  495. // input.QueryParams = reqParams
  496. // output, err := ObsCli.CreateSignedUrl(input)
  497. // if err != nil {
  498. // log.Error("CreateSignedUrl failed:", err.Error())
  499. // return "", err
  500. // }
  501. // return output.SignedUrl, nil
  502. }
  503. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  504. input := &obs.CreateSignedUrlInput{}
  505. input.Method = obs.HttpMethodGet
  506. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  507. input.Bucket = setting.Bucket
  508. input.Expires = 60 * 60
  509. reqParams := make(map[string]string)
  510. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  511. input.QueryParams = reqParams
  512. output, err := ObsCli.CreateSignedUrl(input)
  513. if err != nil {
  514. log.Error("CreateSignedUrl failed:", err.Error())
  515. return "", err
  516. }
  517. return output.SignedUrl, nil
  518. }
  519. func ObsCreateObject(path string) error {
  520. input := &obs.PutObjectInput{}
  521. input.Bucket = setting.Bucket
  522. input.Key = path
  523. _, err := ObsCli.PutObject(input)
  524. if err != nil {
  525. log.Error("PutObject failed:", err.Error())
  526. return err
  527. }
  528. return nil
  529. }