core.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 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. "net/http"
  22. "github.com/minio/minio-go/v7/pkg/encrypt"
  23. )
  24. // Core - Inherits Client and adds new methods to expose the low level S3 APIs.
  25. type Core struct {
  26. *Client
  27. }
  28. // NewCore - Returns new initialized a Core client, this CoreClient should be
  29. // only used under special conditions such as need to access lower primitives
  30. // and being able to use them to write your own wrappers.
  31. func NewCore(endpoint string, opts *Options) (*Core, error) {
  32. var s3Client Core
  33. client, err := New(endpoint, opts)
  34. if err != nil {
  35. return nil, err
  36. }
  37. s3Client.Client = client
  38. return &s3Client, nil
  39. }
  40. // ListObjects - List all the objects at a prefix, optionally with marker and delimiter
  41. // you can further filter the results.
  42. func (c Core) ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (result ListBucketResult, err error) {
  43. return c.listObjectsQuery(context.Background(), bucket, prefix, marker, delimiter, maxKeys, nil)
  44. }
  45. // ListObjectsV2 - Lists all the objects at a prefix, similar to ListObjects() but uses
  46. // continuationToken instead of marker to support iteration over the results.
  47. func (c Core) ListObjectsV2(bucketName, objectPrefix, startAfter, continuationToken, delimiter string, maxkeys int) (ListBucketV2Result, error) {
  48. return c.listObjectsV2Query(context.Background(), bucketName, objectPrefix, continuationToken, true, false, delimiter, startAfter, maxkeys, nil)
  49. }
  50. // CopyObject - copies an object from source object to destination object on server side.
  51. func (c Core) CopyObject(ctx context.Context, sourceBucket, sourceObject, destBucket, destObject string, metadata map[string]string, srcOpts CopySrcOptions, dstOpts PutObjectOptions) (ObjectInfo, error) {
  52. return c.copyObjectDo(ctx, sourceBucket, sourceObject, destBucket, destObject, metadata, srcOpts, dstOpts)
  53. }
  54. // CopyObjectPart - creates a part in a multipart upload by copying (a
  55. // part of) an existing object.
  56. func (c Core) CopyObjectPart(ctx context.Context, srcBucket, srcObject, destBucket, destObject string, uploadID string,
  57. partID int, startOffset, length int64, metadata map[string]string) (p CompletePart, err error) {
  58. return c.copyObjectPartDo(ctx, srcBucket, srcObject, destBucket, destObject, uploadID,
  59. partID, startOffset, length, metadata)
  60. }
  61. // PutObject - Upload object. Uploads using single PUT call.
  62. func (c Core) PutObject(ctx context.Context, bucket, object string, data io.Reader, size int64, md5Base64, sha256Hex string, opts PutObjectOptions) (UploadInfo, error) {
  63. hookReader := newHook(data, opts.Progress)
  64. return c.putObjectDo(ctx, bucket, object, hookReader, md5Base64, sha256Hex, size, opts)
  65. }
  66. // NewMultipartUpload - Initiates new multipart upload and returns the new uploadID.
  67. func (c Core) NewMultipartUpload(ctx context.Context, bucket, object string, opts PutObjectOptions) (uploadID string, err error) {
  68. result, err := c.initiateMultipartUpload(ctx, bucket, object, opts)
  69. return result.UploadID, err
  70. }
  71. // ListMultipartUploads - List incomplete uploads.
  72. func (c Core) ListMultipartUploads(ctx context.Context, bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (result ListMultipartUploadsResult, err error) {
  73. return c.listMultipartUploadsQuery(ctx, bucket, keyMarker, uploadIDMarker, prefix, delimiter, maxUploads)
  74. }
  75. // PutObjectPart - Upload an object part.
  76. func (c Core) PutObjectPart(ctx context.Context, bucket, object, uploadID string, partID int, data io.Reader, size int64, md5Base64, sha256Hex string, sse encrypt.ServerSide) (ObjectPart, error) {
  77. return c.uploadPart(ctx, bucket, object, uploadID, data, partID, md5Base64, sha256Hex, size, sse)
  78. }
  79. // ListObjectParts - List uploaded parts of an incomplete upload.x
  80. func (c Core) ListObjectParts(ctx context.Context, bucket, object, uploadID string, partNumberMarker int, maxParts int) (result ListObjectPartsResult, err error) {
  81. return c.listObjectPartsQuery(ctx, bucket, object, uploadID, partNumberMarker, maxParts)
  82. }
  83. // CompleteMultipartUpload - Concatenate uploaded parts and commit to an object.
  84. func (c Core) CompleteMultipartUpload(ctx context.Context, bucket, object, uploadID string, parts []CompletePart, opts PutObjectOptions) (string, error) {
  85. res, err := c.completeMultipartUpload(ctx, bucket, object, uploadID, completeMultipartUpload{
  86. Parts: parts,
  87. }, opts)
  88. return res.ETag, err
  89. }
  90. // AbortMultipartUpload - Abort an incomplete upload.
  91. func (c Core) AbortMultipartUpload(ctx context.Context, bucket, object, uploadID string) error {
  92. return c.abortMultipartUpload(ctx, bucket, object, uploadID)
  93. }
  94. // GetBucketPolicy - fetches bucket access policy for a given bucket.
  95. func (c Core) GetBucketPolicy(ctx context.Context, bucket string) (string, error) {
  96. return c.getBucketPolicy(ctx, bucket)
  97. }
  98. // PutBucketPolicy - applies a new bucket access policy for a given bucket.
  99. func (c Core) PutBucketPolicy(ctx context.Context, bucket, bucketPolicy string) error {
  100. return c.putBucketPolicy(ctx, bucket, bucketPolicy)
  101. }
  102. // GetObject is a lower level API implemented to support reading
  103. // partial objects and also downloading objects with special conditions
  104. // matching etag, modtime etc.
  105. func (c Core) GetObject(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (io.ReadCloser, ObjectInfo, http.Header, error) {
  106. return c.getObject(ctx, bucketName, objectName, opts)
  107. }
  108. // StatObject is a lower level API implemented to support special
  109. // conditions matching etag, modtime on a request.
  110. func (c Core) StatObject(ctx context.Context, bucketName, objectName string, opts StatObjectOptions) (ObjectInfo, error) {
  111. return c.statObject(ctx, bucketName, objectName, opts)
  112. }