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.

presigned.go 4.7 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package api
  2. import (
  3. "net/http"
  4. "net/url"
  5. "time"
  6. "github.com/google/go-querystring/query"
  7. "gitlink.org.cn/cloudream/jcs-pub/client/sdk/signer"
  8. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  9. )
  10. type PresignedService struct {
  11. *Client
  12. }
  13. func (c *Client) Presigned() *PresignedService {
  14. return &PresignedService{
  15. Client: c,
  16. }
  17. }
  18. const PresignedObjectListByPathPath = "/presigned/object/listByPath"
  19. type PresignedObjectListByPath struct {
  20. ObjectListByPath
  21. }
  22. func (c *PresignedService) ObjectListByPath(req PresignedObjectListByPath, expireIn int) (string, error) {
  23. return c.presign(req, PresignedObjectListByPathPath, http.MethodGet, expireIn)
  24. }
  25. const PresignedObjectDownloadByPathPath = "/presigned/object/downloadByPath"
  26. type PresignedObjectDownloadByPath struct {
  27. PackageID jcstypes.PackageID `form:"packageID" url:"packageID" binding:"required"`
  28. Path string `form:"path" url:"path" binding:"required"`
  29. Offset int64 `form:"offset" url:"offset,omitempty"`
  30. Length *int64 `form:"length" url:"length,omitempty"`
  31. }
  32. func (c *PresignedService) ObjectDownloadByPath(req PresignedObjectDownloadByPath, expireIn int) (string, error) {
  33. return c.presign(req, PresignedObjectDownloadByPathPath, http.MethodGet, expireIn)
  34. }
  35. const PresignedObjectDownloadPath = "/presigned/object/download"
  36. type PresignedObjectDownload struct {
  37. ObjectID jcstypes.ObjectID `form:"objectID" url:"objectID" binding:"required"`
  38. Offset int64 `form:"offset" url:"offset,omitempty"`
  39. Length *int64 `form:"length" url:"length,omitempty"`
  40. }
  41. func (c *PresignedService) ObjectDownload(req PresignedObjectDownload, expireIn int) (string, error) {
  42. return c.presign(req, PresignedObjectDownloadPath, http.MethodGet, expireIn)
  43. }
  44. const PresignedObjectUploadPath = "/presigned/object/upload"
  45. type PresignedObjectUpload struct {
  46. PackageID jcstypes.PackageID `form:"packageID" binding:"required" url:"packageID"`
  47. Path string `form:"path" binding:"required" url:"path"`
  48. Affinity jcstypes.UserSpaceID `form:"affinity" url:"affinity,omitempty"`
  49. CopyTo []jcstypes.UserSpaceID `form:"copyTo" url:"copyTo,omitempty"`
  50. CopyToPath []string `form:"copyToPath" url:"copyToPath,omitempty"`
  51. }
  52. type PresignedObjectUploadResp struct {
  53. Object jcstypes.Object `json:"object"`
  54. }
  55. func (c *PresignedService) ObjectUpload(req PresignedObjectUpload, expireIn int) (string, error) {
  56. return c.presign(req, PresignedObjectUploadPath, http.MethodPost, expireIn)
  57. }
  58. const PresignedObjectNewMultipartUploadPath = "/presigned/object/newMultipartUpload"
  59. type PresignedObjectNewMultipartUpload struct {
  60. PackageID jcstypes.PackageID `form:"packageID" binding:"required" url:"packageID"`
  61. Path string `form:"path" binding:"required" url:"path"`
  62. }
  63. type PresignedObjectNewMultipartUploadResp struct {
  64. Object jcstypes.Object `json:"object"`
  65. }
  66. func (c *PresignedService) ObjectNewMultipartUpload(req PresignedObjectNewMultipartUpload, expireIn int) (string, error) {
  67. return c.presign(req, PresignedObjectNewMultipartUploadPath, http.MethodPost, expireIn)
  68. }
  69. const PresignedObjectUploadPartPath = "/presigned/object/uploadPart"
  70. type PresignedObjectUploadPart struct {
  71. ObjectID jcstypes.ObjectID `form:"objectID" binding:"required" url:"objectID"`
  72. Index int `form:"index" binding:"required" url:"index"`
  73. }
  74. type PresignedUploadPartResp struct{}
  75. func (c *PresignedService) ObjectUploadPart(req PresignedObjectUploadPart, expireIn int) (string, error) {
  76. return c.presign(req, PresignedObjectUploadPartPath, http.MethodPost, expireIn)
  77. }
  78. const PresignedObjectCompleteMultipartUploadPath = "/presigned/object/completeMultipartUpload"
  79. type PresignedObjectCompleteMultipartUpload struct {
  80. ObjectID jcstypes.ObjectID `form:"objectID" binding:"required" url:"objectID"`
  81. Indexes []int `form:"indexes" binding:"required" url:"indexes"`
  82. }
  83. type PresignedObjectCompleteMultipartUploadResp struct {
  84. Object jcstypes.Object `json:"object"`
  85. }
  86. func (c *PresignedService) ObjectCompleteMultipartUpload(req PresignedObjectCompleteMultipartUpload, expireIn int) (string, error) {
  87. return c.presign(req, PresignedObjectCompleteMultipartUploadPath, http.MethodPost, expireIn)
  88. }
  89. func (c *PresignedService) presign(req any, path string, method string, expireIn int) (string, error) {
  90. u, err := url.Parse(c.cfg.EndPoint)
  91. if err != nil {
  92. return "", err
  93. }
  94. u = u.JoinPath("/v1", path)
  95. us, err := query.Values(req)
  96. if err != nil {
  97. return "", err
  98. }
  99. u.RawQuery = us.Encode()
  100. err = signer.PresignURL(c.signKey, method, u, time.Now(), expireIn)
  101. if err != nil {
  102. return "", err
  103. }
  104. return u.String(), nil
  105. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。