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 5.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "fmt"
  7. "io"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "github.com/unknwon/com"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/obs"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. //check if has the object
  17. //todo:修改查询方式
  18. func ObsHasObject(path string) (bool, error) {
  19. hasObject := false
  20. output, err := ObsCli.ListObjects(&obs.ListObjectsInput{Bucket: setting.Bucket})
  21. if err != nil {
  22. log.Error("ListObjects failed:%v", err)
  23. return hasObject, err
  24. }
  25. for _, obj := range output.Contents {
  26. //obj.Key:attachment/0/1/019fd24e-4ef7-41cc-9f85-4a7b8504d958
  27. if path == obj.Key {
  28. hasObject = true
  29. break
  30. }
  31. }
  32. return hasObject, nil
  33. }
  34. func GetObsPartInfos(uuid string, uploadID string) (string, error) {
  35. key := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  36. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  37. Bucket: setting.Bucket,
  38. Key: key,
  39. UploadId: uploadID,
  40. })
  41. if err != nil {
  42. log.Error("ListParts failed:", err.Error())
  43. return "", err
  44. }
  45. var chunks string
  46. for _, partInfo := range output.Parts {
  47. chunks += strconv.Itoa(partInfo.PartNumber) + "-" + partInfo.ETag + ","
  48. }
  49. return chunks, nil
  50. }
  51. func NewObsMultiPartUpload(uuid, fileName string) (string, error) {
  52. input := &obs.InitiateMultipartUploadInput{}
  53. input.Bucket = setting.Bucket
  54. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  55. output, err := ObsCli.InitiateMultipartUpload(input)
  56. if err != nil {
  57. log.Error("InitiateMultipartUpload failed:", err.Error())
  58. return "", err
  59. }
  60. return output.UploadId, nil
  61. }
  62. func CompleteObsMultiPartUpload(uuid, uploadID, fileName string) error {
  63. input := &obs.CompleteMultipartUploadInput{}
  64. input.Bucket = setting.Bucket
  65. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  66. input.UploadId = uploadID
  67. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  68. Bucket: setting.Bucket,
  69. Key: input.Key,
  70. UploadId: uploadID,
  71. })
  72. if err != nil {
  73. log.Error("ListParts failed:", err.Error())
  74. return err
  75. }
  76. for _, partInfo := range output.Parts {
  77. input.Parts = append(input.Parts, obs.Part{
  78. PartNumber: partInfo.PartNumber,
  79. ETag: partInfo.ETag,
  80. })
  81. }
  82. _, err = ObsCli.CompleteMultipartUpload(input)
  83. if err != nil {
  84. log.Error("CompleteMultipartUpload failed:", err.Error())
  85. return err
  86. }
  87. return nil
  88. }
  89. func ObsMultiPartUpload(uuid string, uploadId string, partNumber int, fileName string, putBody io.ReadCloser) error {
  90. input := &obs.UploadPartInput{}
  91. input.Bucket = setting.Bucket
  92. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  93. input.UploadId = uploadId
  94. input.PartNumber = partNumber
  95. input.Body = putBody
  96. output, err := ObsCli.UploadPart(input)
  97. if err == nil {
  98. log.Info("RequestId:%s\n", output.RequestId)
  99. log.Info("ETag:%s\n", output.ETag)
  100. return nil
  101. } else {
  102. if obsError, ok := err.(obs.ObsError); ok {
  103. log.Info(obsError.Code)
  104. log.Info(obsError.Message)
  105. return obsError
  106. } else {
  107. log.Error("error:", err.Error())
  108. return err
  109. }
  110. }
  111. }
  112. func ObsDownload(uuid string, fileName string) (io.ReadCloser, error) {
  113. input := &obs.GetObjectInput{}
  114. input.Bucket = setting.Bucket
  115. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  116. output, err := ObsCli.GetObject(input)
  117. if err == nil {
  118. log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
  119. output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
  120. return output.Body, nil
  121. } else if obsError, ok := err.(obs.ObsError); ok {
  122. fmt.Printf("Code:%s\n", obsError.Code)
  123. fmt.Printf("Message:%s\n", obsError.Message)
  124. return nil, obsError
  125. } else {
  126. return nil, err
  127. }
  128. }
  129. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  130. input := &obs.CreateSignedUrlInput{}
  131. input.Bucket = setting.Bucket
  132. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  133. input.Expires = 60 * 60
  134. input.Method = obs.HttpMethodPut
  135. input.QueryParams = map[string]string{
  136. "partNumber": com.ToStr(partNumber, 10),
  137. "uploadId": uploadId,
  138. //"partSize": com.ToStr(partSize,10),
  139. }
  140. output, err := ObsCli.CreateSignedUrl(input)
  141. if err != nil {
  142. log.Error("CreateSignedUrl failed:", err.Error())
  143. return "", err
  144. }
  145. return output.SignedUrl, nil
  146. }
  147. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  148. input := &obs.CreateSignedUrlInput{}
  149. input.Method = obs.HttpMethodGet
  150. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  151. input.Bucket = setting.Bucket
  152. input.Expires = 60 * 60
  153. reqParams := make(map[string]string)
  154. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  155. input.QueryParams = reqParams
  156. output, err := ObsCli.CreateSignedUrl(input)
  157. if err != nil {
  158. log.Error("CreateSignedUrl failed:", err.Error())
  159. return "", err
  160. }
  161. return output.SignedUrl, nil
  162. }
  163. func ObsCreateObject(path string) error {
  164. input := &obs.PutObjectInput{}
  165. input.Bucket = setting.Bucket
  166. input.Key = path
  167. _, err := ObsCli.PutObject(input)
  168. if err != nil {
  169. log.Error("PutObject failed:", err.Error())
  170. return err
  171. }
  172. return nil
  173. }