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 4.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/obs"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. //check if has the object
  15. //todo:修改查询方式
  16. func ObsHasObject(path string) (bool, error) {
  17. hasObject := false
  18. output, err := ObsCli.ListObjects(&obs.ListObjectsInput{Bucket:setting.Bucket})
  19. if err != nil {
  20. log.Error("ListObjects failed:%v", err)
  21. return hasObject, err
  22. }
  23. for _, obj := range output.Contents {
  24. //obj.Key:attachment/0/1/019fd24e-4ef7-41cc-9f85-4a7b8504d958
  25. if path == obj.Key {
  26. hasObject = true
  27. break
  28. }
  29. }
  30. return hasObject, nil
  31. }
  32. func GetObsPartInfos(uuid string, uploadID string) (string, error) {
  33. key := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  34. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  35. Bucket: setting.Bucket,
  36. Key: key,
  37. UploadId: uploadID,
  38. })
  39. if err != nil {
  40. log.Error("ListParts failed:", err.Error())
  41. return "", err
  42. }
  43. var chunks string
  44. for _, partInfo := range output.Parts {
  45. chunks += strconv.Itoa(partInfo.PartNumber) + "-" + partInfo.ETag + ","
  46. }
  47. return chunks, nil
  48. }
  49. func NewObsMultiPartUpload(uuid string) (string, error) {
  50. input := &obs.InitiateMultipartUploadInput{}
  51. input.Bucket = setting.Bucket
  52. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  53. output, err := ObsCli.InitiateMultipartUpload(input)
  54. if err != nil {
  55. log.Error("InitiateMultipartUpload failed:", err.Error())
  56. return "", err
  57. }
  58. return output.UploadId, nil
  59. }
  60. func CompleteObsMultiPartUpload(uuid string, uploadID string) error {
  61. input := &obs.CompleteMultipartUploadInput{}
  62. input.Bucket = setting.Bucket
  63. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  64. input.UploadId = uploadID
  65. output, err := ObsCli.ListParts(&obs.ListPartsInput{
  66. Bucket: setting.Bucket,
  67. Key: input.Key,
  68. UploadId: uploadID,
  69. })
  70. if err != nil {
  71. log.Error("ListParts failed:", err.Error())
  72. return err
  73. }
  74. for _, partInfo := range output.Parts {
  75. input.Parts = append(input.Parts, obs.Part{
  76. PartNumber: partInfo.PartNumber,
  77. ETag: partInfo.ETag,
  78. })
  79. }
  80. _, err = ObsCli.CompleteMultipartUpload(input)
  81. if err != nil {
  82. log.Error("CompleteMultipartUpload failed:", err.Error())
  83. return err
  84. }
  85. return nil
  86. }
  87. func ObsUploadPart(uuid string, uploadId string, partNumber int, partSize int64, body io.Reader) (string, error) {
  88. input := &obs.UploadPartInput{}
  89. input.PartNumber = partNumber
  90. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  91. input.UploadId = uploadId
  92. input.Bucket = setting.Bucket
  93. input.PartSize = partSize
  94. input.Body = body
  95. output, err := ObsCli.UploadPart(input)
  96. if err != nil {
  97. log.Error("UploadPart failed:", err.Error())
  98. return "", err
  99. }
  100. return output.ETag, nil
  101. }
  102. func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, partSize int64) (string, error) {
  103. /*
  104. input := &obs.CreateSignedUrlInput{}
  105. input.Bucket = setting.Bucket
  106. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid)), "/")
  107. input.Expires = int(PresignedUploadPartUrlExpireTime)
  108. input.Method = obs.HTTP_PUT
  109. input.QueryParams = map[string]string{
  110. "Bucket": input.Bucket,
  111. "Key": input.Key,
  112. "PartNumber": com.ToStr(partNumber,10),
  113. "UploadId": uploadId,
  114. "PartSize": com.ToStr(partSize,10),
  115. }
  116. input.Headers = map[string]string{
  117. }
  118. */
  119. Key := strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  120. req, err := ObsCli.CreateUploadPartSignedUrl(setting.Bucket, Key, uploadId, partNumber, partSize)
  121. if err != nil {
  122. log.Error("CreateSignedUrl failed:", err.Error())
  123. return "", err
  124. }
  125. log.Info(req.URL.String())
  126. log.Info("", req.Header)
  127. return req.URL.String(), nil
  128. }
  129. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  130. input := &obs.CreateSignedUrlInput{}
  131. input.Method = obs.HttpMethodGet
  132. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, uuid)), "/")
  133. input.Bucket = setting.Bucket
  134. input.Expires = 60 * 60
  135. reqParams := make(map[string]string)
  136. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  137. input.QueryParams = reqParams
  138. output, err := ObsCli.CreateSignedUrl(input)
  139. if err != nil {
  140. log.Error("CreateSignedUrl failed:", err.Error())
  141. return "", err
  142. }
  143. return output.SignedUrl, nil
  144. }