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

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