fetch.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package tos
  2. import (
  3. "bytes"
  4. "context"
  5. "net/http"
  6. )
  7. const (
  8. FetchTaskStateFailed = "Failed"
  9. FetchTaskStateSucceed = "Succeed"
  10. FetchTaskStateExpired = "Expired"
  11. FetchTaskStateRunning = "Running"
  12. )
  13. type FetchObjectInput struct {
  14. URL string `json:"URL,omitempty"` // required
  15. Key string `json:"Key,omitempty"` // required
  16. IgnoreSameKey bool `json:"IgnoreSameKey,omitempty"` // optional, default value is false
  17. ContentMD5 string `json:"ContentMD5,omitempty"` // hex-encoded md5, optional
  18. }
  19. type FetchObjectOutput struct {
  20. RequestInfo `json:"-"`
  21. VersionID string `json:"VersionId,omitempty"` // may be empty
  22. ETag string `json:"ETag,omitempty"`
  23. }
  24. type fetchObjectInput struct {
  25. URL string `json:"URL,omitempty"` // required
  26. IgnoreSameKey bool `json:"IgnoreSameKey,omitempty"` // optional, default value is false
  27. ContentMD5 string `json:"ContentMD5,omitempty"` // base64-encoded md5, optional
  28. }
  29. // FetchObject fetch an object from specified URL
  30. // options:
  31. // WithMeta set meta header(s)
  32. // WithServerSideEncryptionCustomer set server side encryption options
  33. // WithACL WithACLGrantFullControl WithACLGrantRead WithACLGrantReadAcp WithACLGrantWrite WithACLGrantWriteAcp set object acl
  34. // Calling FetchObject will be blocked util fetch operation is finished
  35. func (bkt *Bucket) FetchObject(ctx context.Context, input *FetchObjectInput, options ...Option) (*FetchObjectOutput, error) {
  36. if err := isValidKey(input.Key); err != nil {
  37. return nil, err
  38. }
  39. data, contentMD5, err := marshalInput("FetchObjectInput", &fetchObjectInput{
  40. URL: input.URL,
  41. IgnoreSameKey: input.IgnoreSameKey,
  42. ContentMD5: input.ContentMD5,
  43. })
  44. if err != nil {
  45. return nil, err
  46. }
  47. res, err := bkt.client.newBuilder(bkt.name, input.Key, options...).
  48. WithQuery("fetch", "").
  49. WithHeader(HeaderContentMD5, contentMD5).
  50. WithRetry(OnRetryFromStart, ServerErrorClassifier{}).
  51. Request(ctx, http.MethodPost, bytes.NewReader(data), bkt.client.roundTripper(http.StatusOK))
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer res.Close()
  56. out := FetchObjectOutput{RequestInfo: res.RequestInfo()}
  57. if err = marshalOutput(out.RequestID, res.Body, &out); err != nil {
  58. return nil, err
  59. }
  60. out.VersionID = res.Header.Get(HeaderVersionID)
  61. return &out, nil
  62. }
  63. type PutFetchTaskInput struct {
  64. URL string `json:"URL,omitempty"` // required
  65. Object string `json:"Object,omitempty"` // object key, required
  66. IgnoreSameKey bool `json:"IgnoreSameKey,omitempty"` // optional, default value is false
  67. ContentMD5 string `json:"ContentMD5,omitempty"` // hex-encoded md5, optional
  68. }
  69. type PutFetchTaskOutput struct {
  70. RequestInfo `json:"-"`
  71. TaskID string `json:"TaskId,omitempty"`
  72. }
  73. // PutFetchTask put a fetch task to a bucket
  74. // options:
  75. // WithMeta set meta header(s)
  76. // WithServerSideEncryptionCustomer set server side encryption options
  77. // WithACL WithACLGrantFullControl WithACLGrantRead WithACLGrantReadAcp WithACLGrantWrite WithACLGrantWriteAcp set object acl
  78. // Calling PutFetchTask will return immediately after the task created.
  79. func (bkt *Bucket) PutFetchTask(ctx context.Context, input *PutFetchTaskInput, options ...Option) (*PutFetchTaskOutput, error) {
  80. if err := isValidKey(input.Object); err != nil {
  81. return nil, err
  82. }
  83. data, contentMD5, err := marshalInput("PutFetchTaskInput", input)
  84. if err != nil {
  85. return nil, err
  86. }
  87. res, err := bkt.client.newBuilder(bkt.name, "", options...).
  88. WithQuery("fetchTask", "").
  89. WithHeader(HeaderContentMD5, contentMD5).
  90. WithRetry(OnRetryFromStart, ServerErrorClassifier{}).
  91. Request(ctx, http.MethodPost, bytes.NewReader(data), bkt.client.roundTripper(http.StatusOK))
  92. if err != nil {
  93. return nil, err
  94. }
  95. defer res.Close()
  96. out := PutFetchTaskOutput{RequestInfo: res.RequestInfo()}
  97. if err = marshalOutput(out.RequestID, res.Body, &out); err != nil {
  98. return nil, err
  99. }
  100. return &out, nil
  101. }
  102. type GetFetchTaskInput struct {
  103. TaskID string `json:"taskID,omitempty"`
  104. }
  105. type GetFetchTaskOutput struct {
  106. RequestInfo `json:"-"`
  107. State string `json:"State,omitempty"`
  108. // Cause string `json:"Cause,omitempty"`
  109. }
  110. // GetFetchTask query the task state by the TaskID
  111. // Task state:
  112. // FetchTaskStateFailed = "Failed"
  113. // FetchTaskStateSucceed = "Succeed"
  114. // FetchTaskStateExpired = "Expired"
  115. // FetchTaskStateRunning = "Running"
  116. func (bkt *Bucket) GetFetchTask(ctx context.Context, input *GetFetchTaskInput, options ...Option) (*GetFetchTaskOutput, error) {
  117. res, err := bkt.client.newBuilder(bkt.name, "", options...).
  118. WithQuery("fetchTask", "").
  119. WithQuery("taskId", input.TaskID).
  120. WithRetry(nil, StatusCodeClassifier{}).
  121. Request(ctx, http.MethodGet, nil, bkt.client.roundTripper(http.StatusOK))
  122. if err != nil {
  123. return nil, err
  124. }
  125. defer res.Close()
  126. out := GetFetchTaskOutput{RequestInfo: res.RequestInfo()}
  127. if err = marshalOutput(out.RequestID, res.Body, &out); err != nil {
  128. return nil, err
  129. }
  130. return &out, nil
  131. }