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.

305 lines
13 KiB

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