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.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "github.com/unknwon/com"
  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, fileName 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, fileName)), "/")
  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, uploadID, fileName 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, fileName)), "/")
  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 ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) {
  88. input := &obs.CreateSignedUrlInput{}
  89. input.Bucket = setting.Bucket
  90. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  91. input.Expires = 60 * 60
  92. input.Method = obs.HttpMethodPut
  93. input.QueryParams = map[string]string{
  94. "partNumber": com.ToStr(partNumber, 10),
  95. "uploadId": uploadId,
  96. //"partSize": com.ToStr(partSize,10),
  97. }
  98. output, err := ObsCli.CreateSignedUrl(input)
  99. if err != nil {
  100. log.Error("CreateSignedUrl failed:", err.Error())
  101. return "", err
  102. }
  103. return output.SignedUrl, nil
  104. }
  105. func ObsGetPreSignedUrl(uuid, fileName string) (string, error) {
  106. input := &obs.CreateSignedUrlInput{}
  107. input.Method = obs.HttpMethodGet
  108. input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/")
  109. input.Bucket = setting.Bucket
  110. input.Expires = 60 * 60
  111. reqParams := make(map[string]string)
  112. reqParams["response-content-disposition"] = "attachment; filename=\"" + fileName + "\""
  113. input.QueryParams = reqParams
  114. output, err := ObsCli.CreateSignedUrl(input)
  115. if err != nil {
  116. log.Error("CreateSignedUrl failed:", err.Error())
  117. return "", err
  118. }
  119. return output.SignedUrl, nil
  120. }