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.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 index, val := range output.Contents {
  161. log.Info("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n",
  162. index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size)
  163. str1 := strings.Split(val.Key, "/")
  164. fileName := str1[len(str1)-1]
  165. log.Info("", fileName)
  166. fileInfo := FileInfo{
  167. ModTime: val.LastModified.Format("2006-01-02 15:04:05"),
  168. FileName: fileName,
  169. Size: val.Size,
  170. IsDir:false,
  171. }
  172. fileInfos = append(fileInfos, fileInfo)
  173. }
  174. return fileInfos, err
  175. } else {
  176. if obsError, ok := err.(obs.ObsError); ok {
  177. log.Error("Code:%s, Message:%s", obsError.Code, obsError.Message)
  178. }
  179. return nil, err
  180. }
  181. }
  182. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  183. input := &obs.CreateSignedUrlInput{}
  184. input.Bucket = setting.Bucket
  185. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  186. input.Expires = 60 * 60
  187. input.Method = obs.HttpMethodPut
  188. input.QueryParams = map[string]string{
  189. "partNumber": com.ToStr(partNumber, 10),
  190. "uploadId": uploadId,
  191. //"partSize": com.ToStr(partSize,10),
  192. }
  193. output, err := ObsCli.CreateSignedUrl(input)
  194. if err != nil {
  195. log.Error("CreateSignedUrl failed:", err.Error())
  196. return "", err
  197. }
  198. return output.SignedUrl, nil
  199. }
  200. func GetObsCreateSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  201. input := &obs.CreateSignedUrlInput{}
  202. input.Bucket = setting.Bucket
  203. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  204. input.Expires = 60 * 60
  205. input.Method = obs.HttpMethodPut
  206. input.QueryParams = map[string]string{
  207. "partNumber": com.ToStr(partNumber, 10),
  208. "uploadId": uploadId,
  209. //"partSize": com.ToStr(partSize,10),
  210. }
  211. output, err := ObsCli.CreateSignedUrl(input)
  212. if err != nil {
  213. log.Error("CreateSignedUrl failed:", err.Error())
  214. return "", err
  215. }
  216. return output.SignedUrl, nil
  217. }
  218. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  219. input := &obs.CreateSignedUrlInput{}
  220. input.Method = obs.HttpMethodGet
  221. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  222. input.Bucket = setting.Bucket
  223. input.Expires = 60 * 60
  224. reqParams := make(map[string]string)
  225. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  226. input.QueryParams = reqParams
  227. output, err := ObsCli.CreateSignedUrl(input)
  228. if err != nil {
  229. log.Error("CreateSignedUrl failed:", err.Error())
  230. return "", err
  231. }
  232. return output.SignedUrl, nil
  233. }
  234. func ObsCreateObject(path string) error {
  235. input := &obs.PutObjectInput{}
  236. input.Bucket = setting.Bucket
  237. input.Key = path
  238. _, err := ObsCli.PutObject(input)
  239. if err != nil {
  240. log.Error("PutObject failed:", err.Error())
  241. return err
  242. }
  243. return nil
  244. }