api-copy-object.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2017, 2018 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "context"
  20. "io"
  21. "io/ioutil"
  22. "net/http"
  23. )
  24. // CopyObject - copy a source object into a new object
  25. func (c Client) CopyObject(ctx context.Context, dst CopyDestOptions, src CopySrcOptions) (UploadInfo, error) {
  26. if err := src.validate(); err != nil {
  27. return UploadInfo{}, err
  28. }
  29. if err := dst.validate(); err != nil {
  30. return UploadInfo{}, err
  31. }
  32. header := make(http.Header)
  33. dst.Marshal(header)
  34. src.Marshal(header)
  35. resp, err := c.executeMethod(ctx, http.MethodPut, requestMetadata{
  36. bucketName: dst.Bucket,
  37. objectName: dst.Object,
  38. customHeader: header,
  39. })
  40. if err != nil {
  41. return UploadInfo{}, err
  42. }
  43. defer closeResponse(resp)
  44. if resp.StatusCode != http.StatusOK {
  45. return UploadInfo{}, httpRespToErrorResponse(resp, dst.Bucket, dst.Object)
  46. }
  47. // Update the progress properly after successful copy.
  48. if dst.Progress != nil {
  49. io.Copy(ioutil.Discard, io.LimitReader(dst.Progress, dst.Size))
  50. }
  51. cpObjRes := copyObjectResult{}
  52. if err = xmlDecoder(resp.Body, &cpObjRes); err != nil {
  53. return UploadInfo{}, err
  54. }
  55. // extract lifecycle expiry date and rule ID
  56. expTime, ruleID := amzExpirationToExpiryDateRuleID(resp.Header.Get(amzExpiration))
  57. return UploadInfo{
  58. Bucket: dst.Bucket,
  59. Key: dst.Object,
  60. LastModified: cpObjRes.LastModified,
  61. ETag: trimEtag(resp.Header.Get("ETag")),
  62. VersionID: resp.Header.Get(amzVersionID),
  63. Expiration: expTime,
  64. ExpirationRuleID: ruleID,
  65. }, nil
  66. }