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.

244 lines
10 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. )
  10. // InitiateMultipartUploadOptions is the option of InitateMultipartUpload
  11. type InitiateMultipartUploadOptions struct {
  12. *ACLHeaderOptions
  13. *ObjectPutHeaderOptions
  14. }
  15. // InitiateMultipartUploadResult is the result of InitateMultipartUpload
  16. type InitiateMultipartUploadResult struct {
  17. XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
  18. Bucket string
  19. Key string
  20. UploadID string `xml:"UploadId"`
  21. }
  22. // InitiateMultipartUpload 请求实现初始化分片上传,成功执行此请求以后会返回Upload ID用于后续的Upload Part请求。
  23. //
  24. // https://www.qcloud.com/document/product/436/7746
  25. func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string, opt *InitiateMultipartUploadOptions) (*InitiateMultipartUploadResult, *Response, error) {
  26. var res InitiateMultipartUploadResult
  27. sendOpt := sendOptions{
  28. baseURL: s.client.BaseURL.BucketURL,
  29. uri: "/" + encodeURIComponent(name) + "?uploads",
  30. method: http.MethodPost,
  31. optHeader: opt,
  32. result: &res,
  33. }
  34. resp, err := s.client.send(ctx, &sendOpt)
  35. return &res, resp, err
  36. }
  37. // ObjectUploadPartOptions is the options of upload-part
  38. type ObjectUploadPartOptions struct {
  39. Expect string `header:"Expect,omitempty" url:"-"`
  40. XCosContentSHA1 string `header:"x-cos-content-sha1" url:"-"`
  41. ContentLength int `header:"Content-Length,omitempty" url:"-"`
  42. XCosSSECustomerAglo string `header:"x-cos-server-side-encryption-customer-algorithm,omitempty" url:"-" xml:"-"`
  43. XCosSSECustomerKey string `header:"x-cos-server-side-encryption-customer-key,omitempty" url:"-" xml:"-"`
  44. XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
  45. }
  46. // UploadPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  47. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  48. //
  49. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  50. //
  51. // 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ContentLength
  52. //
  53. // https://www.qcloud.com/document/product/436/7750
  54. func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error) {
  55. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  56. sendOpt := sendOptions{
  57. baseURL: s.client.BaseURL.BucketURL,
  58. uri: u,
  59. method: http.MethodPut,
  60. optHeader: opt,
  61. body: r,
  62. }
  63. resp, err := s.client.send(ctx, &sendOpt)
  64. return resp, err
  65. }
  66. // ObjectListPartsOptions is the option of ListParts
  67. type ObjectListPartsOptions struct {
  68. EncodingType string `url:"Encoding-type,omitempty"`
  69. MaxParts string `url:"max-parts,omitempty"`
  70. PartNumberMarker string `url:"part-number-marker,omitempty"`
  71. }
  72. // ObjectListPartsResult is the result of ListParts
  73. type ObjectListPartsResult struct {
  74. XMLName xml.Name `xml:"ListPartsResult"`
  75. Bucket string
  76. EncodingType string `xml:"Encoding-type,omitempty"`
  77. Key string
  78. UploadID string `xml:"UploadId"`
  79. Initiator *Initiator `xml:"Initiator,omitempty"`
  80. Owner *Owner `xml:"Owner,omitempty"`
  81. StorageClass string
  82. PartNumberMarker string
  83. NextPartNumberMarker string `xml:"NextPartNumberMarker,omitempty"`
  84. MaxParts string
  85. IsTruncated bool
  86. Parts []Object `xml:"Part,omitempty"`
  87. }
  88. // ListParts 用来查询特定分块上传中的已上传的块。
  89. //
  90. // https://www.qcloud.com/document/product/436/7747
  91. func (s *ObjectService) ListParts(ctx context.Context, name, uploadID string, opt *ObjectListPartsOptions) (*ObjectListPartsResult, *Response, error) {
  92. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  93. var res ObjectListPartsResult
  94. sendOpt := sendOptions{
  95. baseURL: s.client.BaseURL.BucketURL,
  96. uri: u,
  97. method: http.MethodGet,
  98. result: &res,
  99. optQuery: opt,
  100. }
  101. resp, err := s.client.send(ctx, &sendOpt)
  102. return &res, resp, err
  103. }
  104. // CompleteMultipartUploadOptions is the option of CompleteMultipartUpload
  105. type CompleteMultipartUploadOptions struct {
  106. XMLName xml.Name `xml:"CompleteMultipartUpload"`
  107. Parts []Object `xml:"Part"`
  108. }
  109. // CompleteMultipartUploadResult is the result CompleteMultipartUpload
  110. type CompleteMultipartUploadResult struct {
  111. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  112. Location string
  113. Bucket string
  114. Key string
  115. ETag string
  116. }
  117. // ObjectList can used for sort the parts which needs in complete upload part
  118. // sort.Sort(cos.ObjectList(opt.Parts))
  119. type ObjectList []Object
  120. func (o ObjectList) Len() int {
  121. return len(o)
  122. }
  123. func (o ObjectList) Swap(i, j int) {
  124. o[i], o[j] = o[j], o[i]
  125. }
  126. func (o ObjectList) Less(i, j int) bool { // rewrite the Less method from small to big
  127. return o[i].PartNumber < o[j].PartNumber
  128. }
  129. // CompleteMultipartUpload 用来实现完成整个分块上传。当您已经使用Upload Parts上传所有块以后,你可以用该API完成上传。
  130. // 在使用该API时,您必须在Body中给出每一个块的PartNumber和ETag,用来校验块的准确性。
  131. //
  132. // 由于分块上传的合并需要数分钟时间,因而当合并分块开始的时候,COS就立即返回200的状态码,在合并的过程中,
  133. // COS会周期性的返回空格信息来保持连接活跃,直到合并完成,COS会在Body中返回合并后块的内容。
  134. //
  135. // 当上传块小于1 MB的时候,在调用该请求时,会返回400 EntityTooSmall;
  136. // 当上传块编号不连续的时候,在调用该请求时,会返回400 InvalidPart;
  137. // 当请求Body中的块信息没有按序号从小到大排列的时候,在调用该请求时,会返回400 InvalidPartOrder;
  138. // 当UploadId不存在的时候,在调用该请求时,会返回404 NoSuchUpload。
  139. //
  140. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  141. //
  142. // https://www.qcloud.com/document/product/436/7742
  143. func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, name, uploadID string, opt *CompleteMultipartUploadOptions) (*CompleteMultipartUploadResult, *Response, error) {
  144. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  145. var res CompleteMultipartUploadResult
  146. sendOpt := sendOptions{
  147. baseURL: s.client.BaseURL.BucketURL,
  148. uri: u,
  149. method: http.MethodPost,
  150. body: opt,
  151. result: &res,
  152. }
  153. resp, err := s.client.send(ctx, &sendOpt)
  154. // If the error occurs during the copy operation, the error response is embedded in the 200 OK response. This means that a 200 OK response can contain either a success or an error.
  155. if err == nil && resp.StatusCode == 200 {
  156. if res.ETag == "" {
  157. return &res, resp, errors.New("response 200 OK, but body contains an error")
  158. }
  159. }
  160. return &res, resp, err
  161. }
  162. // AbortMultipartUpload 用来实现舍弃一个分块上传并删除已上传的块。当您调用Abort Multipart Upload时,
  163. // 如果有正在使用这个Upload Parts上传块的请求,则Upload Parts会返回失败。当该UploadID不存在时,会返回404 NoSuchUpload。
  164. //
  165. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  166. //
  167. // https://www.qcloud.com/document/product/436/7740
  168. func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string) (*Response, error) {
  169. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  170. sendOpt := sendOptions{
  171. baseURL: s.client.BaseURL.BucketURL,
  172. uri: u,
  173. method: http.MethodDelete,
  174. }
  175. resp, err := s.client.send(ctx, &sendOpt)
  176. return resp, err
  177. }
  178. // ObjectCopyPartOptions is the options of copy-part
  179. type ObjectCopyPartOptions struct {
  180. XCosCopySource string `header:"x-cos-copy-source" url:"-"`
  181. XCosCopySourceRange string `header:"x-cos-copy-source-range,omitempty" url:"-"`
  182. XCosCopySourceIfModifiedSince string `header:"x-cos-copy-source-If-Modified-Since,omitempty" url:"-"`
  183. XCosCopySourceIfUnmodifiedSince string `header:"x-cos-copy-source-If-Unmodified-Since,omitempty" url:"-"`
  184. XCosCopySourceIfMatch string `header:"x-cos-copy-source-If-Match,omitempty" url:"-"`
  185. XCosCopySourceIfNoneMatch string `header:"x-cos-copy-source-If-None-Match,omitempty" url:"-"`
  186. }
  187. // CopyPartResult is the result CopyPart
  188. type CopyPartResult struct {
  189. XMLName xml.Name `xml:"CopyPartResult"`
  190. ETag string
  191. LastModified string
  192. }
  193. // CopyPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  194. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  195. // ObjectCopyPartOptions的XCosCopySource为必填参数,格式为<bucket-name>-<app-id>.cos.<region-id>.myqcloud.com/<object-key>
  196. // ObjectCopyPartOptions的XCosCopySourceRange指定源的Range,格式为bytes=<start>-<end>
  197. //
  198. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  199. //
  200. // https://www.qcloud.com/document/product/436/7750
  201. func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, partNumber int, sourceURL string, opt *ObjectCopyPartOptions) (*CopyPartResult, *Response, error) {
  202. if opt == nil {
  203. opt = &ObjectCopyPartOptions{}
  204. }
  205. opt.XCosCopySource = sourceURL
  206. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  207. var res CopyPartResult
  208. sendOpt := sendOptions{
  209. baseURL: s.client.BaseURL.BucketURL,
  210. uri: u,
  211. method: http.MethodPut,
  212. optHeader: opt,
  213. result: &res,
  214. }
  215. resp, err := s.client.send(ctx, &sendOpt)
  216. // If the error occurs during the copy operation, the error response is embedded in the 200 OK response. This means that a 200 OK response can contain either a success or an error.
  217. if err == nil && resp != nil && resp.StatusCode == 200 {
  218. if res.ETag == "" {
  219. return &res, resp, errors.New("response 200 OK, but body contains an error")
  220. }
  221. }
  222. return &res, resp, err
  223. }