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.

246 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" header:"-" url:"-"`
  107. Parts []Object `xml:"Part" header:"-" url:"-"`
  108. XOptionHeader *http.Header `header:"-,omitempty" xml:"-" url:"-"`
  109. }
  110. // CompleteMultipartUploadResult is the result CompleteMultipartUpload
  111. type CompleteMultipartUploadResult struct {
  112. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  113. Location string
  114. Bucket string
  115. Key string
  116. ETag string
  117. }
  118. // ObjectList can used for sort the parts which needs in complete upload part
  119. // sort.Sort(cos.ObjectList(opt.Parts))
  120. type ObjectList []Object
  121. func (o ObjectList) Len() int {
  122. return len(o)
  123. }
  124. func (o ObjectList) Swap(i, j int) {
  125. o[i], o[j] = o[j], o[i]
  126. }
  127. func (o ObjectList) Less(i, j int) bool { // rewrite the Less method from small to big
  128. return o[i].PartNumber < o[j].PartNumber
  129. }
  130. // CompleteMultipartUpload 用来实现完成整个分块上传。当您已经使用Upload Parts上传所有块以后,你可以用该API完成上传。
  131. // 在使用该API时,您必须在Body中给出每一个块的PartNumber和ETag,用来校验块的准确性。
  132. //
  133. // 由于分块上传的合并需要数分钟时间,因而当合并分块开始的时候,COS就立即返回200的状态码,在合并的过程中,
  134. // COS会周期性的返回空格信息来保持连接活跃,直到合并完成,COS会在Body中返回合并后块的内容。
  135. //
  136. // 当上传块小于1 MB的时候,在调用该请求时,会返回400 EntityTooSmall;
  137. // 当上传块编号不连续的时候,在调用该请求时,会返回400 InvalidPart;
  138. // 当请求Body中的块信息没有按序号从小到大排列的时候,在调用该请求时,会返回400 InvalidPartOrder;
  139. // 当UploadId不存在的时候,在调用该请求时,会返回404 NoSuchUpload。
  140. //
  141. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  142. //
  143. // https://www.qcloud.com/document/product/436/7742
  144. func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, name, uploadID string, opt *CompleteMultipartUploadOptions) (*CompleteMultipartUploadResult, *Response, error) {
  145. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  146. var res CompleteMultipartUploadResult
  147. sendOpt := sendOptions{
  148. baseURL: s.client.BaseURL.BucketURL,
  149. uri: u,
  150. method: http.MethodPost,
  151. optHeader: opt,
  152. body: opt,
  153. result: &res,
  154. }
  155. resp, err := s.client.send(ctx, &sendOpt)
  156. // 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.
  157. if err == nil && resp.StatusCode == 200 {
  158. if res.ETag == "" {
  159. return &res, resp, errors.New("response 200 OK, but body contains an error")
  160. }
  161. }
  162. return &res, resp, err
  163. }
  164. // AbortMultipartUpload 用来实现舍弃一个分块上传并删除已上传的块。当您调用Abort Multipart Upload时,
  165. // 如果有正在使用这个Upload Parts上传块的请求,则Upload Parts会返回失败。当该UploadID不存在时,会返回404 NoSuchUpload。
  166. //
  167. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  168. //
  169. // https://www.qcloud.com/document/product/436/7740
  170. func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string) (*Response, error) {
  171. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  172. sendOpt := sendOptions{
  173. baseURL: s.client.BaseURL.BucketURL,
  174. uri: u,
  175. method: http.MethodDelete,
  176. }
  177. resp, err := s.client.send(ctx, &sendOpt)
  178. return resp, err
  179. }
  180. // ObjectCopyPartOptions is the options of copy-part
  181. type ObjectCopyPartOptions struct {
  182. XCosCopySource string `header:"x-cos-copy-source" url:"-"`
  183. XCosCopySourceRange string `header:"x-cos-copy-source-range,omitempty" url:"-"`
  184. XCosCopySourceIfModifiedSince string `header:"x-cos-copy-source-If-Modified-Since,omitempty" url:"-"`
  185. XCosCopySourceIfUnmodifiedSince string `header:"x-cos-copy-source-If-Unmodified-Since,omitempty" url:"-"`
  186. XCosCopySourceIfMatch string `header:"x-cos-copy-source-If-Match,omitempty" url:"-"`
  187. XCosCopySourceIfNoneMatch string `header:"x-cos-copy-source-If-None-Match,omitempty" url:"-"`
  188. }
  189. // CopyPartResult is the result CopyPart
  190. type CopyPartResult struct {
  191. XMLName xml.Name `xml:"CopyPartResult"`
  192. ETag string
  193. LastModified string
  194. }
  195. // CopyPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  196. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  197. // ObjectCopyPartOptions的XCosCopySource为必填参数,格式为<bucket-name>-<app-id>.cos.<region-id>.myqcloud.com/<object-key>
  198. // ObjectCopyPartOptions的XCosCopySourceRange指定源的Range,格式为bytes=<start>-<end>
  199. //
  200. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  201. //
  202. // https://www.qcloud.com/document/product/436/7750
  203. func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, partNumber int, sourceURL string, opt *ObjectCopyPartOptions) (*CopyPartResult, *Response, error) {
  204. if opt == nil {
  205. opt = &ObjectCopyPartOptions{}
  206. }
  207. opt.XCosCopySource = sourceURL
  208. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  209. var res CopyPartResult
  210. sendOpt := sendOptions{
  211. baseURL: s.client.BaseURL.BucketURL,
  212. uri: u,
  213. method: http.MethodPut,
  214. optHeader: opt,
  215. result: &res,
  216. }
  217. resp, err := s.client.send(ctx, &sendOpt)
  218. // 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.
  219. if err == nil && resp != nil && resp.StatusCode == 200 {
  220. if res.ETag == "" {
  221. return &res, resp, errors.New("response 200 OK, but body contains an error")
  222. }
  223. }
  224. return &res, resp, err
  225. }