You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.8 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. )
  9. // InitiateMultipartUploadOptions is the option of InitateMultipartUpload
  10. type InitiateMultipartUploadOptions struct {
  11. *ACLHeaderOptions
  12. *ObjectPutHeaderOptions
  13. }
  14. // InitiateMultipartUploadResult is the result of InitateMultipartUpload
  15. type InitiateMultipartUploadResult struct {
  16. XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
  17. Bucket string
  18. Key string
  19. UploadID string `xml:"UploadId"`
  20. }
  21. // InitiateMultipartUpload 请求实现初始化分片上传,成功执行此请求以后会返回Upload ID用于后续的Upload Part请求。
  22. //
  23. // https://www.qcloud.com/document/product/436/7746
  24. func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string, opt *InitiateMultipartUploadOptions) (*InitiateMultipartUploadResult, *Response, error) {
  25. var res InitiateMultipartUploadResult
  26. sendOpt := sendOptions{
  27. baseURL: s.client.BaseURL.BucketURL,
  28. uri: "/" + encodeURIComponent(name) + "?uploads",
  29. method: http.MethodPost,
  30. optHeader: opt,
  31. result: &res,
  32. }
  33. resp, err := s.client.send(ctx, &sendOpt)
  34. return &res, resp, err
  35. }
  36. // ObjectUploadPartOptions is the options of upload-part
  37. type ObjectUploadPartOptions struct {
  38. Expect string `header:"Expect,omitempty" url:"-"`
  39. XCosContentSHA1 string `header:"x-cos-content-sha1" url:"-"`
  40. ContentLength int `header:"Content-Length,omitempty" url:"-"`
  41. }
  42. // UploadPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  43. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  44. //
  45. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  46. //
  47. // 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ContentLength
  48. //
  49. // https://www.qcloud.com/document/product/436/7750
  50. func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error) {
  51. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  52. sendOpt := sendOptions{
  53. baseURL: s.client.BaseURL.BucketURL,
  54. uri: u,
  55. method: http.MethodPut,
  56. optHeader: opt,
  57. body: r,
  58. }
  59. resp, err := s.client.send(ctx, &sendOpt)
  60. return resp, err
  61. }
  62. // ObjectListPartsOptions is the option of ListParts
  63. type ObjectListPartsOptions struct {
  64. EncodingType string `url:"Encoding-type,omitempty"`
  65. MaxParts int `url:"max-parts,omitempty"`
  66. PartNumberMarker int `url:"part-number-marker,omitempty"`
  67. }
  68. // ObjectListPartsResult is the result of ListParts
  69. type ObjectListPartsResult struct {
  70. XMLName xml.Name `xml:"ListPartsResult"`
  71. Bucket string
  72. EncodingType string `xml:"Encoding-type,omitempty"`
  73. Key string
  74. UploadID string `xml:"UploadId"`
  75. Initiator *Initiator `xml:"Initiator,omitempty"`
  76. Owner *Owner `xml:"Owner,omitempty"`
  77. StorageClass string
  78. PartNumberMarker int
  79. NextPartNumberMarker int `xml:"NextPartNumberMarker,omitempty"`
  80. MaxParts int
  81. IsTruncated bool
  82. Parts []Object `xml:"Part,omitempty"`
  83. }
  84. // ListParts 用来查询特定分块上传中的已上传的块。
  85. //
  86. // https://www.qcloud.com/document/product/436/7747
  87. func (s *ObjectService) ListParts(ctx context.Context, name, uploadID string) (*ObjectListPartsResult, *Response, error) {
  88. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  89. var res ObjectListPartsResult
  90. sendOpt := sendOptions{
  91. baseURL: s.client.BaseURL.BucketURL,
  92. uri: u,
  93. method: http.MethodGet,
  94. result: &res,
  95. }
  96. resp, err := s.client.send(ctx, &sendOpt)
  97. return &res, resp, err
  98. }
  99. // CompleteMultipartUploadOptions is the option of CompleteMultipartUpload
  100. type CompleteMultipartUploadOptions struct {
  101. XMLName xml.Name `xml:"CompleteMultipartUpload"`
  102. Parts []Object `xml:"Part"`
  103. }
  104. // CompleteMultipartUploadResult is the result CompleteMultipartUpload
  105. type CompleteMultipartUploadResult struct {
  106. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  107. Location string
  108. Bucket string
  109. Key string
  110. ETag string
  111. }
  112. // CompleteMultipartUpload 用来实现完成整个分块上传。当您已经使用Upload Parts上传所有块以后,你可以用该API完成上传。
  113. // 在使用该API时,您必须在Body中给出每一个块的PartNumber和ETag,用来校验块的准确性。
  114. //
  115. // 由于分块上传的合并需要数分钟时间,因而当合并分块开始的时候,COS就立即返回200的状态码,在合并的过程中,
  116. // COS会周期性的返回空格信息来保持连接活跃,直到合并完成,COS会在Body中返回合并后块的内容。
  117. //
  118. // 当上传块小于1 MB的时候,在调用该请求时,会返回400 EntityTooSmall;
  119. // 当上传块编号不连续的时候,在调用该请求时,会返回400 InvalidPart;
  120. // 当请求Body中的块信息没有按序号从小到大排列的时候,在调用该请求时,会返回400 InvalidPartOrder;
  121. // 当UploadId不存在的时候,在调用该请求时,会返回404 NoSuchUpload。
  122. //
  123. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  124. //
  125. // https://www.qcloud.com/document/product/436/7742
  126. func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, name, uploadID string, opt *CompleteMultipartUploadOptions) (*CompleteMultipartUploadResult, *Response, error) {
  127. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  128. var res CompleteMultipartUploadResult
  129. sendOpt := sendOptions{
  130. baseURL: s.client.BaseURL.BucketURL,
  131. uri: u,
  132. method: http.MethodPost,
  133. body: opt,
  134. result: &res,
  135. }
  136. resp, err := s.client.send(ctx, &sendOpt)
  137. return &res, resp, err
  138. }
  139. // AbortMultipartUpload 用来实现舍弃一个分块上传并删除已上传的块。当您调用Abort Multipart Upload时,
  140. // 如果有正在使用这个Upload Parts上传块的请求,则Upload Parts会返回失败。当该UploadID不存在时,会返回404 NoSuchUpload。
  141. //
  142. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  143. //
  144. // https://www.qcloud.com/document/product/436/7740
  145. func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string) (*Response, error) {
  146. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  147. sendOpt := sendOptions{
  148. baseURL: s.client.BaseURL.BucketURL,
  149. uri: u,
  150. method: http.MethodDelete,
  151. }
  152. resp, err := s.client.send(ctx, &sendOpt)
  153. return resp, err
  154. }