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

4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 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
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 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
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 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
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
2 years ago
2 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. "net/url"
  9. "path"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/obs"
  15. "code.gitea.io/gitea/modules/setting"
  16. "github.com/unknwon/com"
  17. )
  18. type FileInfo struct {
  19. FileName string `json:"FileName"`
  20. ModTime string `json:"ModTime"`
  21. IsDir bool `json:"IsDir"`
  22. Size int64 `json:"Size"`
  23. ParenDir string `json:"ParenDir"`
  24. UUID string `json:"UUID"`
  25. }
  26. type FileInfoList []FileInfo
  27. const MAX_LIST_PARTS = 1000
  28. func (ulist FileInfoList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
  29. func (ulist FileInfoList) Len() int { return len(ulist) }
  30. func (ulist FileInfoList) Less(i, j int) bool {
  31. return strings.Compare(ulist[i].FileName, ulist[j].FileName) > 0
  32. }
  33. //check if has the object
  34. func ObsHasObject(path string) (bool, error) {
  35. hasObject := false
  36. input := &obs.GetObjectMetadataInput{}
  37. input.Bucket = setting.Bucket
  38. input.Key = path
  39. _, err := ObsCli.GetObjectMetadata(input)
  40. if err == nil {
  41. hasObject = true
  42. } else {
  43. if obsError, ok := err.(obs.ObsError); ok {
  44. log.Error("GetObjectMetadata failed(%d): %s", obsError.StatusCode, obsError.Message)
  45. } else {
  46. log.Error("%v", err.Error())
  47. }
  48. }
  49. return hasObject, nil
  50. }
  51. func listAllParts(uuid, uploadID, key string) (output *obs.ListPartsOutput, err error) {
  52. output = &obs.ListPartsOutput{}
  53. partNumberMarker := 0
  54. for {
  55. temp, err := ObsCli.ListParts(&obs.ListPartsInput{
  56. Bucket: setting.Bucket,
  57. Key: key,
  58. UploadId: uploadID,
  59. MaxParts: MAX_LIST_PARTS,
  60. PartNumberMarker: partNumberMarker,
  61. })
  62. if err != nil {
  63. log.Error("ListParts failed:", err.Error())
  64. return output, err
  65. }
  66. partNumberMarker = temp.NextPartNumberMarker
  67. log.Info("uuid:%s, MaxParts:%d, PartNumberMarker:%d, NextPartNumberMarker:%d, len:%d", uuid, temp.MaxParts, temp.PartNumberMarker, temp.NextPartNumberMarker, len(temp.Parts))
  68. for _, partInfo := range temp.Parts {
  69. output.Parts = append(output.Parts, obs.Part{
  70. PartNumber: partInfo.PartNumber,
  71. ETag: partInfo.ETag,
  72. })
  73. }
  74. if !temp.IsTruncated {
  75. break
  76. } else {
  77. continue
  78. }
  79. }
  80. return output, nil
  81. }
  82. func GetObsPartInfos(objectName, uploadID string) (string, error) {
  83. key := objectName
  84. //strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  85. allParts, err := listAllParts(objectName, uploadID, key)
  86. if err != nil {
  87. log.Error("listAllParts failed: %v", err)
  88. return "", err
  89. }
  90. var chunks string
  91. for _, partInfo := range allParts.Parts {
  92. chunks += strconv.Itoa(partInfo.PartNumber) + "-" + partInfo.ETag + ","
  93. }
  94. return chunks, nil
  95. }
  96. func NewObsMultiPartUpload(objectName string) (string, error) {
  97. input := &obs.InitiateMultipartUploadInput{}
  98. input.Bucket = setting.Bucket
  99. input.Key = objectName
  100. //strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  101. output, err := ObsCli.InitiateMultipartUpload(input)
  102. if err != nil {
  103. log.Error("InitiateMultipartUpload failed:", err.Error())
  104. return "", err
  105. }
  106. return output.UploadId, nil
  107. }
  108. func CompleteObsMultiPartUpload(objectName, uploadID string, totalChunks int) error {
  109. input := &obs.CompleteMultipartUploadInput{}
  110. input.Bucket = setting.Bucket
  111. //input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  112. input.Key = objectName
  113. input.UploadId = uploadID
  114. allParts, err := listAllParts(objectName, uploadID, input.Key)
  115. if err != nil {
  116. log.Error("listAllParts failed: %v", err)
  117. return err
  118. }
  119. if len(allParts.Parts) != totalChunks {
  120. log.Error("listAllParts number(%d) is not equal the set total chunk number(%d)", len(allParts.Parts), totalChunks)
  121. return errors.New("the parts is not complete")
  122. }
  123. input.Parts = allParts.Parts
  124. output, err := ObsCli.CompleteMultipartUpload(input)
  125. if err != nil {
  126. log.Error("CompleteMultipartUpload failed:", err.Error())
  127. return err
  128. }
  129. log.Info("uuid:%s, RequestId:%s", objectName, output.RequestId)
  130. return nil
  131. }
  132. func ObsMultiPartUpload(objectName string, uploadId string, partNumber int, fileName string, putBody io.ReadCloser) error {
  133. input := &obs.UploadPartInput{}
  134. input.Bucket = setting.Bucket
  135. input.Key = objectName
  136. //strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  137. input.UploadId = uploadId
  138. input.PartNumber = partNumber
  139. input.Body = putBody
  140. output, err := ObsCli.UploadPart(input)
  141. if err == nil {
  142. log.Info("RequestId:%s\n", output.RequestId)
  143. log.Info("ETag:%s\n", output.ETag)
  144. return nil
  145. } else {
  146. if obsError, ok := err.(obs.ObsError); ok {
  147. log.Info(obsError.Code)
  148. log.Info(obsError.Message)
  149. return obsError
  150. } else {
  151. log.Error("error:", err.Error())
  152. return err
  153. }
  154. }
  155. }
  156. //delete all file under the dir path
  157. func ObsRemoveObject(bucket string, path string) error {
  158. log.Info("Bucket=" + bucket + " path=" + path)
  159. if len(path) == 0 {
  160. return errors.New("path canot be null.")
  161. }
  162. input := &obs.ListObjectsInput{}
  163. input.Bucket = bucket
  164. // 设置每页100个对象
  165. input.MaxKeys = 100
  166. input.Prefix = path
  167. index := 1
  168. log.Info("prefix=" + input.Prefix)
  169. for {
  170. output, err := ObsCli.ListObjects(input)
  171. if err == nil {
  172. log.Info("Page:%d\n", index)
  173. index++
  174. for _, val := range output.Contents {
  175. log.Info("delete obs file:" + val.Key)
  176. delObj := &obs.DeleteObjectInput{}
  177. delObj.Bucket = setting.Bucket
  178. delObj.Key = val.Key
  179. ObsCli.DeleteObject(delObj)
  180. }
  181. if output.IsTruncated {
  182. input.Marker = output.NextMarker
  183. } else {
  184. break
  185. }
  186. } else {
  187. if obsError, ok := err.(obs.ObsError); ok {
  188. log.Info("Code:%s\n", obsError.Code)
  189. log.Info("Message:%s\n", obsError.Message)
  190. }
  191. return err
  192. }
  193. }
  194. return nil
  195. }
  196. func ObsDownloadAFile(bucket string, key string) (io.ReadCloser, error) {
  197. input := &obs.GetObjectInput{}
  198. input.Bucket = bucket
  199. input.Key = key
  200. output, err := ObsCli.GetObject(input)
  201. if err == nil {
  202. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  203. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  204. return output.Body, nil
  205. } else if obsError, ok := err.(obs.ObsError); ok {
  206. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  207. return nil, obsError
  208. } else {
  209. return nil, err
  210. }
  211. }
  212. func ObsModelDownload(JobName string, fileName string) (io.ReadCloser, error) {
  213. input := &obs.GetObjectInput{}
  214. input.Bucket = setting.Bucket
  215. input.Key = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, JobName, setting.OutPutPath, fileName), "/")
  216. // input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  217. output, err := ObsCli.GetObject(input)
  218. if err == nil {
  219. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  220. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  221. return output.Body, nil
  222. } else if obsError, ok := err.(obs.ObsError); ok {
  223. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  224. return nil, obsError
  225. } else {
  226. return nil, err
  227. }
  228. }
  229. func ObsGetFilesSize(srcBucket string, Files []string) int64 {
  230. var fileTotalSize int64
  231. for _, file := range Files {
  232. log.Info("file=" + file)
  233. out, err := ObsCli.GetObjectMetadata(&obs.GetObjectMetadataInput{
  234. Bucket: srcBucket,
  235. Key: file,
  236. })
  237. if err != nil {
  238. log.Info("Get File error, error=" + err.Error())
  239. continue
  240. }
  241. fileTotalSize += out.ContentLength
  242. }
  243. return fileTotalSize
  244. }
  245. func ObsCopyManyFile(srcBucket string, srcPath string, destBucket string, destPath string, Files []string) (int64, error) {
  246. var fileTotalSize int64
  247. for _, file := range Files {
  248. srcKey := srcPath + file
  249. destKey := destPath + file
  250. log.Info("srcKey=" + srcKey + " destKey=" + destKey)
  251. out, err := ObsCli.GetObjectMetadata(&obs.GetObjectMetadataInput{
  252. Bucket: srcBucket,
  253. Key: srcKey,
  254. })
  255. if err != nil {
  256. log.Info("Get File error, error=" + err.Error())
  257. continue
  258. }
  259. ObsCopyFile(srcBucket, srcKey, destBucket, destKey)
  260. fileTotalSize += out.ContentLength
  261. }
  262. return fileTotalSize, nil
  263. }
  264. func ObsCopyAllFile(srcBucket string, srcPath string, destBucket string, destPath string) (int64, error) {
  265. input := &obs.ListObjectsInput{}
  266. input.Bucket = srcBucket
  267. // 设置每页100个对象
  268. input.MaxKeys = 100
  269. input.Prefix = srcPath
  270. index := 1
  271. length := len(srcPath)
  272. var fileTotalSize int64
  273. log.Info("prefix=" + input.Prefix)
  274. for {
  275. output, err := ObsCli.ListObjects(input)
  276. if err == nil {
  277. log.Info("Page:%d\n", index)
  278. index++
  279. for _, val := range output.Contents {
  280. destKey := destPath + val.Key[length:]
  281. ObsCopyFile(srcBucket, val.Key, destBucket, destKey)
  282. fileTotalSize += val.Size
  283. }
  284. if output.IsTruncated {
  285. input.Marker = output.NextMarker
  286. } else {
  287. break
  288. }
  289. } else {
  290. if obsError, ok := err.(obs.ObsError); ok {
  291. log.Info("Code:%s\n", obsError.Code)
  292. log.Info("Message:%s\n", obsError.Message)
  293. }
  294. return 0, err
  295. }
  296. }
  297. return fileTotalSize, nil
  298. }
  299. func ObsCopyFile(srcBucket string, srcKeyName string, destBucket string, destKeyName string) error {
  300. input := &obs.CopyObjectInput{}
  301. input.Bucket = destBucket
  302. input.Key = destKeyName
  303. input.CopySourceBucket = srcBucket
  304. input.CopySourceKey = srcKeyName
  305. _, err := ObsCli.CopyObject(input)
  306. if err == nil {
  307. log.Info("copy success,destBuckName:%s, destkeyname:%s", destBucket, destKeyName)
  308. } else {
  309. log.Info("copy failed,,destBuckName:%s, destkeyname:%s", destBucket, destKeyName)
  310. if obsError, ok := err.(obs.ObsError); ok {
  311. log.Info(obsError.Code)
  312. log.Info(obsError.Message)
  313. }
  314. return err
  315. }
  316. return nil
  317. }
  318. func GetOneLevelAllObjectUnderDir(bucket string, prefixRootPath string, relativePath string) ([]FileInfo, error) {
  319. input := &obs.ListObjectsInput{}
  320. input.Bucket = bucket
  321. input.Prefix = prefixRootPath + relativePath
  322. if !strings.HasSuffix(input.Prefix, "/") {
  323. input.Prefix += "/"
  324. }
  325. fileInfos := make([]FileInfo, 0)
  326. prefixLen := len(input.Prefix)
  327. fileMap := make(map[string]bool, 0)
  328. index := 1
  329. for {
  330. output, err := ObsCli.ListObjects(input)
  331. if err == nil {
  332. log.Info("Page:%d\n", index)
  333. index++
  334. for _, val := range output.Contents {
  335. var isDir bool
  336. var fileName string
  337. if val.Key == input.Prefix {
  338. continue
  339. }
  340. fileName = val.Key[prefixLen:]
  341. files := strings.Split(fileName, "/")
  342. if fileMap[files[0]] {
  343. continue
  344. } else {
  345. fileMap[files[0]] = true
  346. }
  347. ParenDir := relativePath
  348. fileName = files[0]
  349. if len(files) > 1 {
  350. isDir = true
  351. ParenDir += fileName + "/"
  352. } else {
  353. isDir = false
  354. }
  355. fileInfo := FileInfo{
  356. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  357. FileName: fileName,
  358. Size: val.Size,
  359. IsDir: isDir,
  360. ParenDir: ParenDir,
  361. }
  362. fileInfos = append(fileInfos, fileInfo)
  363. }
  364. if output.IsTruncated {
  365. input.Marker = output.NextMarker
  366. } else {
  367. break
  368. }
  369. } else {
  370. if obsError, ok := err.(obs.ObsError); ok {
  371. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  372. }
  373. return nil, err
  374. }
  375. }
  376. return fileInfos, nil
  377. }
  378. func GetAllObjectByBucketAndPrefix(bucket string, prefix string) ([]FileInfo, error) {
  379. input := &obs.ListObjectsInput{}
  380. input.Bucket = bucket
  381. // 设置每页100个对象
  382. input.MaxKeys = 100
  383. input.Prefix = prefix
  384. index := 1
  385. fileInfoList := FileInfoList{}
  386. prefixLen := len(prefix)
  387. log.Info("prefix=" + input.Prefix)
  388. for {
  389. output, err := ObsCli.ListObjects(input)
  390. if err == nil {
  391. log.Info("Page:%d\n", index)
  392. index++
  393. for _, val := range output.Contents {
  394. var isDir bool
  395. if prefixLen == len(val.Key) {
  396. continue
  397. }
  398. if strings.HasSuffix(val.Key, "/") {
  399. isDir = true
  400. } else {
  401. isDir = false
  402. }
  403. fileInfo := FileInfo{
  404. ModTime: val.LastModified.Format("2006-01-02 15:04:05"),
  405. FileName: val.Key[prefixLen:],
  406. Size: val.Size,
  407. IsDir: isDir,
  408. ParenDir: "",
  409. }
  410. fileInfoList = append(fileInfoList, fileInfo)
  411. }
  412. if output.IsTruncated {
  413. input.Marker = output.NextMarker
  414. } else {
  415. break
  416. }
  417. } else {
  418. if obsError, ok := err.(obs.ObsError); ok {
  419. log.Info("Code:%s\n", obsError.Code)
  420. log.Info("Message:%s\n", obsError.Message)
  421. }
  422. return nil, err
  423. }
  424. }
  425. sort.Sort(fileInfoList)
  426. return fileInfoList, nil
  427. }
  428. func GetObsListObject(jobName, outPutPath, parentDir, versionName string) ([]FileInfo, error) {
  429. input := &obs.ListObjectsInput{}
  430. input.Bucket = setting.Bucket
  431. input.Prefix = strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, outPutPath, versionName, parentDir), "/")
  432. if !strings.HasSuffix(input.Prefix, "/") {
  433. input.Prefix += "/"
  434. }
  435. output, err := ObsCli.ListObjects(input)
  436. fileInfos := make([]FileInfo, 0)
  437. prefixLen := len(input.Prefix)
  438. fileMap := make(map[string]bool, 0)
  439. if err == nil {
  440. for _, val := range output.Contents {
  441. log.Info("val key=" + val.Key)
  442. var isDir bool
  443. var fileName string
  444. if val.Key == input.Prefix {
  445. continue
  446. }
  447. fileName = val.Key[prefixLen:]
  448. log.Info("fileName =" + fileName)
  449. files := strings.Split(fileName, "/")
  450. if fileMap[files[0]] {
  451. continue
  452. } else {
  453. fileMap[files[0]] = true
  454. }
  455. ParenDir := parentDir
  456. fileName = files[0]
  457. if len(files) > 1 {
  458. isDir = true
  459. ParenDir += fileName + "/"
  460. } else {
  461. isDir = false
  462. }
  463. fileInfo := FileInfo{
  464. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  465. FileName: fileName,
  466. Size: val.Size,
  467. IsDir: isDir,
  468. ParenDir: ParenDir,
  469. }
  470. fileInfos = append(fileInfos, fileInfo)
  471. }
  472. sort.Slice(fileInfos, func(i, j int) bool {
  473. return fileInfos[i].ModTime > fileInfos[j].ModTime
  474. })
  475. return fileInfos, err
  476. } else {
  477. if obsError, ok := err.(obs.ObsError); ok {
  478. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  479. }
  480. return nil, err
  481. }
  482. }
  483. func ObsGenMultiPartSignedUrl(objectName string, uploadId string, partNumber int) (string, error) {
  484. input := &obs.CreateSignedUrlInput{}
  485. input.Bucket = setting.Bucket
  486. input.Key = objectName
  487. //strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  488. input.Expires = 60 * 60
  489. input.Method = obs.HttpMethodPut
  490. input.QueryParams = map[string]string{
  491. "partNumber": com.ToStr(partNumber, 10),
  492. "uploadId": uploadId,
  493. //"partSize": com.ToStr(partSize,10),
  494. }
  495. output, err := ObsCli.CreateSignedUrl(input)
  496. if err != nil {
  497. log.Error("CreateSignedUrl failed:", err.Error())
  498. return "", err
  499. }
  500. return output.SignedUrl, nil
  501. }
  502. func GetObsCreateSignedUrlByBucketAndKey(bucket, key string) (string, error) {
  503. input := &obs.CreateSignedUrlInput{}
  504. input.Bucket = bucket
  505. input.Key = key
  506. input.Expires = 60 * 60
  507. input.Method = obs.HttpMethodGet
  508. comma := strings.LastIndex(key, "/")
  509. filename := key
  510. if comma != -1 {
  511. filename = key[comma+1:]
  512. }
  513. reqParams := make(map[string]string)
  514. filename = url.PathEscape(filename)
  515. reqParams["response-content-disposition"] = "attachment; filename=\"" + filename + "\""
  516. input.QueryParams = reqParams
  517. output, err := ObsCli.CreateSignedUrl(input)
  518. if err != nil {
  519. log.Error("CreateSignedUrl failed:", err.Error())
  520. return "", err
  521. }
  522. return output.SignedUrl, nil
  523. }
  524. func GetObsCreateSignedUrl(jobName, parentDir, fileName string) (string, error) {
  525. return GetObsCreateSignedUrlByBucketAndKey(setting.Bucket, strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir, fileName), "/"))
  526. }
  527. func ObsGetPreSignedUrl(objectName, fileName string) (string, error) {
  528. input := &obs.CreateSignedUrlInput{}
  529. input.Method = obs.HttpMethodGet
  530. input.Key = objectName
  531. //strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  532. input.Bucket = setting.Bucket
  533. input.Expires = 60 * 60
  534. fileName = url.PathEscape(fileName)
  535. reqParams := make(map[string]string)
  536. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  537. input.QueryParams = reqParams
  538. output, err := ObsCli.CreateSignedUrl(input)
  539. if err != nil {
  540. log.Error("CreateSignedUrl failed:", err.Error())
  541. return "", err
  542. }
  543. return output.SignedUrl, nil
  544. }
  545. func ObsCreateObject(path string) error {
  546. input := &obs.PutObjectInput{}
  547. input.Bucket = setting.Bucket
  548. input.Key = path
  549. _, err := ObsCli.PutObject(input)
  550. if err != nil {
  551. log.Error("PutObject failed:", err.Error())
  552. return err
  553. }
  554. return nil
  555. }
  556. func GetObsLogFileName(prefix string) ([]FileInfo, error) {
  557. input := &obs.ListObjectsInput{}
  558. input.Bucket = setting.Bucket
  559. input.Prefix = prefix
  560. output, err := ObsCli.ListObjects(input)
  561. if err != nil {
  562. log.Error("PutObject failed:", err.Error())
  563. return nil, err
  564. }
  565. if output == nil || len(output.Contents) == 0 {
  566. return nil, errors.New("obs log files not exist")
  567. }
  568. fileInfos := make([]FileInfo, 0)
  569. for _, val := range output.Contents {
  570. //result[num] = c.Key
  571. if strings.HasSuffix(val.Key, ".log") {
  572. log.Info("log fileName=" + val.Key)
  573. fileInfo := FileInfo{
  574. ModTime: val.LastModified.Local().Format("2006-01-02 15:04:05"),
  575. FileName: val.Key[len(prefix)-3:], //加上 job
  576. Size: val.Size,
  577. IsDir: false,
  578. ParenDir: prefix[0 : len(prefix)-3],
  579. }
  580. fileInfos = append(fileInfos, fileInfo)
  581. }
  582. }
  583. return fileInfos, nil
  584. }
  585. func IsObjectExist4Obs(bucket, key string) (bool, error) {
  586. _, err := ObsCli.GetObjectMetadata(&obs.GetObjectMetadataInput{
  587. Bucket: bucket,
  588. Key: key,
  589. })
  590. if err != nil {
  591. log.Error("GetObjectMetadata error.%v", err)
  592. return false, err
  593. }
  594. return true, nil
  595. }