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.

295 lines
13 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,omitempty" 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. XCosTrafficLimit int `header:"x-cos-traffic-limit,omitempty" url:"-" xml:"-"`
  46. }
  47. // UploadPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  48. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  49. //
  50. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  51. //
  52. // 当 r 不是 bytes.Buffer/bytes.Reader/strings.Reader 时,必须指定 opt.ContentLength
  53. //
  54. // https://www.qcloud.com/document/product/436/7750
  55. func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error) {
  56. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  57. sendOpt := sendOptions{
  58. baseURL: s.client.BaseURL.BucketURL,
  59. uri: u,
  60. method: http.MethodPut,
  61. optHeader: opt,
  62. body: r,
  63. }
  64. resp, err := s.client.send(ctx, &sendOpt)
  65. return resp, err
  66. }
  67. // ObjectListPartsOptions is the option of ListParts
  68. type ObjectListPartsOptions struct {
  69. EncodingType string `url:"Encoding-type,omitempty"`
  70. MaxParts string `url:"max-parts,omitempty"`
  71. PartNumberMarker string `url:"part-number-marker,omitempty"`
  72. }
  73. // ObjectListPartsResult is the result of ListParts
  74. type ObjectListPartsResult struct {
  75. XMLName xml.Name `xml:"ListPartsResult"`
  76. Bucket string
  77. EncodingType string `xml:"Encoding-type,omitempty"`
  78. Key string
  79. UploadID string `xml:"UploadId"`
  80. Initiator *Initiator `xml:"Initiator,omitempty"`
  81. Owner *Owner `xml:"Owner,omitempty"`
  82. StorageClass string
  83. PartNumberMarker string
  84. NextPartNumberMarker string `xml:"NextPartNumberMarker,omitempty"`
  85. MaxParts string
  86. IsTruncated bool
  87. Parts []Object `xml:"Part,omitempty"`
  88. }
  89. // ListParts 用来查询特定分块上传中的已上传的块。
  90. //
  91. // https://www.qcloud.com/document/product/436/7747
  92. func (s *ObjectService) ListParts(ctx context.Context, name, uploadID string, opt *ObjectListPartsOptions) (*ObjectListPartsResult, *Response, error) {
  93. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  94. var res ObjectListPartsResult
  95. sendOpt := sendOptions{
  96. baseURL: s.client.BaseURL.BucketURL,
  97. uri: u,
  98. method: http.MethodGet,
  99. result: &res,
  100. optQuery: opt,
  101. }
  102. resp, err := s.client.send(ctx, &sendOpt)
  103. return &res, resp, err
  104. }
  105. // CompleteMultipartUploadOptions is the option of CompleteMultipartUpload
  106. type CompleteMultipartUploadOptions struct {
  107. XMLName xml.Name `xml:"CompleteMultipartUpload" header:"-" url:"-"`
  108. Parts []Object `xml:"Part" header:"-" url:"-"`
  109. XOptionHeader *http.Header `header:"-,omitempty" xml:"-" url:"-"`
  110. }
  111. // CompleteMultipartUploadResult is the result CompleteMultipartUpload
  112. type CompleteMultipartUploadResult struct {
  113. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  114. Location string
  115. Bucket string
  116. Key string
  117. ETag string
  118. }
  119. // ObjectList can used for sort the parts which needs in complete upload part
  120. // sort.Sort(cos.ObjectList(opt.Parts))
  121. type ObjectList []Object
  122. func (o ObjectList) Len() int {
  123. return len(o)
  124. }
  125. func (o ObjectList) Swap(i, j int) {
  126. o[i], o[j] = o[j], o[i]
  127. }
  128. func (o ObjectList) Less(i, j int) bool { // rewrite the Less method from small to big
  129. return o[i].PartNumber < o[j].PartNumber
  130. }
  131. // CompleteMultipartUpload 用来实现完成整个分块上传。当您已经使用Upload Parts上传所有块以后,你可以用该API完成上传。
  132. // 在使用该API时,您必须在Body中给出每一个块的PartNumber和ETag,用来校验块的准确性。
  133. //
  134. // 由于分块上传的合并需要数分钟时间,因而当合并分块开始的时候,COS就立即返回200的状态码,在合并的过程中,
  135. // COS会周期性的返回空格信息来保持连接活跃,直到合并完成,COS会在Body中返回合并后块的内容。
  136. //
  137. // 当上传块小于1 MB的时候,在调用该请求时,会返回400 EntityTooSmall;
  138. // 当上传块编号不连续的时候,在调用该请求时,会返回400 InvalidPart;
  139. // 当请求Body中的块信息没有按序号从小到大排列的时候,在调用该请求时,会返回400 InvalidPartOrder;
  140. // 当UploadId不存在的时候,在调用该请求时,会返回404 NoSuchUpload。
  141. //
  142. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  143. //
  144. // https://www.qcloud.com/document/product/436/7742
  145. func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, name, uploadID string, opt *CompleteMultipartUploadOptions) (*CompleteMultipartUploadResult, *Response, error) {
  146. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  147. var res CompleteMultipartUploadResult
  148. sendOpt := sendOptions{
  149. baseURL: s.client.BaseURL.BucketURL,
  150. uri: u,
  151. method: http.MethodPost,
  152. optHeader: opt,
  153. body: opt,
  154. result: &res,
  155. }
  156. resp, err := s.client.send(ctx, &sendOpt)
  157. // 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.
  158. if err == nil && resp.StatusCode == 200 {
  159. if res.ETag == "" {
  160. return &res, resp, errors.New("response 200 OK, but body contains an error")
  161. }
  162. }
  163. return &res, resp, err
  164. }
  165. // AbortMultipartUpload 用来实现舍弃一个分块上传并删除已上传的块。当您调用Abort Multipart Upload时,
  166. // 如果有正在使用这个Upload Parts上传块的请求,则Upload Parts会返回失败。当该UploadID不存在时,会返回404 NoSuchUpload。
  167. //
  168. // 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
  169. //
  170. // https://www.qcloud.com/document/product/436/7740
  171. func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string) (*Response, error) {
  172. u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
  173. sendOpt := sendOptions{
  174. baseURL: s.client.BaseURL.BucketURL,
  175. uri: u,
  176. method: http.MethodDelete,
  177. }
  178. resp, err := s.client.send(ctx, &sendOpt)
  179. return resp, err
  180. }
  181. // ObjectCopyPartOptions is the options of copy-part
  182. type ObjectCopyPartOptions struct {
  183. XCosCopySource string `header:"x-cos-copy-source" url:"-"`
  184. XCosCopySourceRange string `header:"x-cos-copy-source-range,omitempty" url:"-"`
  185. XCosCopySourceIfModifiedSince string `header:"x-cos-copy-source-If-Modified-Since,omitempty" url:"-"`
  186. XCosCopySourceIfUnmodifiedSince string `header:"x-cos-copy-source-If-Unmodified-Since,omitempty" url:"-"`
  187. XCosCopySourceIfMatch string `header:"x-cos-copy-source-If-Match,omitempty" url:"-"`
  188. XCosCopySourceIfNoneMatch string `header:"x-cos-copy-source-If-None-Match,omitempty" url:"-"`
  189. }
  190. // CopyPartResult is the result CopyPart
  191. type CopyPartResult struct {
  192. XMLName xml.Name `xml:"CopyPartResult"`
  193. ETag string
  194. LastModified string
  195. }
  196. // CopyPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
  197. // 在每次请求Upload Part时候,需要携带partNumber和uploadID,partNumber为块的编号,支持乱序上传。
  198. // ObjectCopyPartOptions的XCosCopySource为必填参数,格式为<bucket-name>-<app-id>.cos.<region-id>.myqcloud.com/<object-key>
  199. // ObjectCopyPartOptions的XCosCopySourceRange指定源的Range,格式为bytes=<start>-<end>
  200. //
  201. // 当传入uploadID和partNumber都相同的时候,后传入的块将覆盖之前传入的块。当uploadID不存在时会返回404错误,NoSuchUpload.
  202. //
  203. // https://www.qcloud.com/document/product/436/7750
  204. func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, partNumber int, sourceURL string, opt *ObjectCopyPartOptions) (*CopyPartResult, *Response, error) {
  205. if opt == nil {
  206. opt = &ObjectCopyPartOptions{}
  207. }
  208. opt.XCosCopySource = sourceURL
  209. u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
  210. var res CopyPartResult
  211. sendOpt := sendOptions{
  212. baseURL: s.client.BaseURL.BucketURL,
  213. uri: u,
  214. method: http.MethodPut,
  215. optHeader: opt,
  216. result: &res,
  217. }
  218. resp, err := s.client.send(ctx, &sendOpt)
  219. // 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.
  220. if err == nil && resp != nil && resp.StatusCode == 200 {
  221. if res.ETag == "" {
  222. return &res, resp, errors.New("response 200 OK, but body contains an error")
  223. }
  224. }
  225. return &res, resp, err
  226. }
  227. type ObjectListUploadsOptions struct {
  228. Delimiter string `url:"Delimiter,omitempty"`
  229. EncodingType string `url:"EncodingType,omitempty"`
  230. Prefix string `url:"Prefix"`
  231. MaxUploads int `url:"MaxUploads"`
  232. KeyMarker string `url:"KeyMarker"`
  233. UploadIdMarker string `url:"UploadIDMarker"`
  234. }
  235. type ObjectListUploadsResult struct {
  236. XMLName xml.Name `xml:"ListMultipartUploadsResult"`
  237. Bucket string `xml:"Bucket,omitempty"`
  238. EncodingType string `xml:"Encoding-Type,omitempty"`
  239. KeyMarker string `xml:"KeyMarker,omitempty"`
  240. UploadIdMarker string `xml:"UploadIdMarker,omitempty"`
  241. NextKeyMarker string `xml:"NextKeyMarker,omitempty"`
  242. NextUploadIdMarker string `xml:"NextUploadIdMarker,omitempty"`
  243. MaxUploads string `xml:"MaxUploads,omitempty"`
  244. IsTruncated bool `xml:"IsTruncated,omitempty"`
  245. Prefix string `xml:"Prefix,omitempty"`
  246. Delimiter string `xml:"Delimiter,omitempty"`
  247. Upload []ListUploadsResultUpload `xml:"Upload,omitempty"`
  248. CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
  249. }
  250. type ListUploadsResultUpload struct {
  251. Key string `xml:"Key,omitempty"`
  252. UploadID string `xml:"UploadId,omitempty"`
  253. StorageClass string `xml:"StorageClass,omitempty"`
  254. Initiator *Initiator `xml:"Initiator,omitempty"`
  255. Owner *Owner `xml:"Owner,omitempty"`
  256. Initiated string `xml:"Initiated,omitempty"`
  257. }
  258. func (s *ObjectService) ListUploads(ctx context.Context, opt *ObjectListUploadsOptions) (*ObjectListUploadsResult, *Response, error) {
  259. var res ObjectListUploadsResult
  260. sendOpt := &sendOptions{
  261. baseURL: s.client.BaseURL.BucketURL,
  262. uri: "/?uploads",
  263. method: http.MethodGet,
  264. optQuery: opt,
  265. result: &res,
  266. }
  267. resp, err := s.client.send(ctx, sendOpt)
  268. return &res, resp, err
  269. }