github.com/minio/minio-go/v6@v6.0.57/core.go (about) 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 18 package minio 19 20 import ( 21 "context" 22 "io" 23 "net/http" 24 25 "github.com/minio/minio-go/v6/pkg/encrypt" 26 ) 27 28 // Core - Inherits Client and adds new methods to expose the low level S3 APIs. 29 type Core struct { 30 *Client 31 } 32 33 // NewCore - Returns new initialized a Core client, this CoreClient should be 34 // only used under special conditions such as need to access lower primitives 35 // and being able to use them to write your own wrappers. 36 func NewCore(endpoint string, accessKeyID, secretAccessKey string, secure bool) (*Core, error) { 37 var s3Client Core 38 client, err := NewV4(endpoint, accessKeyID, secretAccessKey, secure) 39 if err != nil { 40 return nil, err 41 } 42 s3Client.Client = client 43 return &s3Client, nil 44 } 45 46 // ListObjects - List all the objects at a prefix, optionally with marker and delimiter 47 // you can further filter the results. 48 func (c Core) ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (result ListBucketResult, err error) { 49 return c.listObjectsQuery(bucket, prefix, marker, delimiter, maxKeys) 50 } 51 52 // ListObjectsV2 - Lists all the objects at a prefix, similar to ListObjects() but uses 53 // continuationToken instead of marker to support iteration over the results. 54 func (c Core) ListObjectsV2(bucketName, objectPrefix, continuationToken string, fetchOwner bool, delimiter string, maxkeys int, startAfter string) (ListBucketV2Result, error) { 55 return c.listObjectsV2Query(bucketName, objectPrefix, continuationToken, fetchOwner, false, delimiter, maxkeys, startAfter) 56 } 57 58 // CopyObjectWithContext - copies an object from source object to destination object on server side. 59 func (c Core) CopyObjectWithContext(ctx context.Context, sourceBucket, sourceObject, destBucket, destObject string, metadata map[string]string) (ObjectInfo, error) { 60 return c.copyObjectDo(ctx, sourceBucket, sourceObject, destBucket, destObject, metadata) 61 } 62 63 // CopyObject - copies an object from source object to destination object on server side. 64 func (c Core) CopyObject(sourceBucket, sourceObject, destBucket, destObject string, metadata map[string]string) (ObjectInfo, error) { 65 return c.CopyObjectWithContext(context.Background(), sourceBucket, sourceObject, destBucket, destObject, metadata) 66 } 67 68 // CopyObjectPartWithContext - creates a part in a multipart upload by copying (a 69 // part of) an existing object. 70 func (c Core) CopyObjectPartWithContext(ctx context.Context, srcBucket, srcObject, destBucket, destObject string, uploadID string, 71 partID int, startOffset, length int64, metadata map[string]string) (p CompletePart, err error) { 72 73 return c.copyObjectPartDo(ctx, srcBucket, srcObject, destBucket, destObject, uploadID, 74 partID, startOffset, length, metadata) 75 } 76 77 // CopyObjectPart - creates a part in a multipart upload by copying (a 78 // part of) an existing object. 79 func (c Core) CopyObjectPart(srcBucket, srcObject, destBucket, destObject string, uploadID string, 80 partID int, startOffset, length int64, metadata map[string]string) (p CompletePart, err error) { 81 82 return c.CopyObjectPartWithContext(context.Background(), srcBucket, srcObject, destBucket, destObject, uploadID, 83 partID, startOffset, length, metadata) 84 } 85 86 // PutObjectWithContext - Upload object. Uploads using single PUT call. 87 func (c Core) PutObjectWithContext(ctx context.Context, bucket, object string, data io.Reader, size int64, md5Base64, sha256Hex string, opts PutObjectOptions) (ObjectInfo, error) { 88 return c.putObjectDo(ctx, bucket, object, data, md5Base64, sha256Hex, size, opts) 89 } 90 91 // PutObject - Upload object. Uploads using single PUT call. 92 func (c Core) PutObject(bucket, object string, data io.Reader, size int64, md5Base64, sha256Hex string, opts PutObjectOptions) (ObjectInfo, error) { 93 return c.PutObjectWithContext(context.Background(), bucket, object, data, size, md5Base64, sha256Hex, opts) 94 } 95 96 // NewMultipartUpload - Initiates new multipart upload and returns the new uploadID. 97 func (c Core) NewMultipartUpload(bucket, object string, opts PutObjectOptions) (uploadID string, err error) { 98 result, err := c.initiateMultipartUpload(context.Background(), bucket, object, opts) 99 return result.UploadID, err 100 } 101 102 // ListMultipartUploads - List incomplete uploads. 103 func (c Core) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (result ListMultipartUploadsResult, err error) { 104 return c.listMultipartUploadsQuery(bucket, keyMarker, uploadIDMarker, prefix, delimiter, maxUploads) 105 } 106 107 // PutObjectPartWithContext - Upload an object part. 108 func (c Core) PutObjectPartWithContext(ctx context.Context, bucket, object, uploadID string, partID int, data io.Reader, size int64, md5Base64, sha256Hex string, sse encrypt.ServerSide) (ObjectPart, error) { 109 return c.uploadPart(ctx, bucket, object, uploadID, data, partID, md5Base64, sha256Hex, size, sse) 110 } 111 112 // PutObjectPart - Upload an object part. 113 func (c Core) PutObjectPart(bucket, object, uploadID string, partID int, data io.Reader, size int64, md5Base64, sha256Hex string, sse encrypt.ServerSide) (ObjectPart, error) { 114 return c.PutObjectPartWithContext(context.Background(), bucket, object, uploadID, partID, data, size, md5Base64, sha256Hex, sse) 115 } 116 117 // ListObjectParts - List uploaded parts of an incomplete upload.x 118 func (c Core) ListObjectParts(bucket, object, uploadID string, partNumberMarker int, maxParts int) (result ListObjectPartsResult, err error) { 119 return c.listObjectPartsQuery(bucket, object, uploadID, partNumberMarker, maxParts) 120 } 121 122 // CompleteMultipartUploadWithContext - Concatenate uploaded parts and commit to an object. 123 func (c Core) CompleteMultipartUploadWithContext(ctx context.Context, bucket, object, uploadID string, parts []CompletePart) (string, error) { 124 res, err := c.completeMultipartUpload(ctx, bucket, object, uploadID, completeMultipartUpload{ 125 Parts: parts, 126 }) 127 return res.ETag, err 128 } 129 130 // CompleteMultipartUpload - Concatenate uploaded parts and commit to an object. 131 func (c Core) CompleteMultipartUpload(bucket, object, uploadID string, parts []CompletePart) (string, error) { 132 return c.CompleteMultipartUploadWithContext(context.Background(), bucket, object, uploadID, parts) 133 } 134 135 // AbortMultipartUploadWithContext - Abort an incomplete upload. 136 func (c Core) AbortMultipartUploadWithContext(ctx context.Context, bucket, object, uploadID string) error { 137 return c.abortMultipartUpload(ctx, bucket, object, uploadID) 138 } 139 140 // AbortMultipartUpload - Abort an incomplete upload. 141 func (c Core) AbortMultipartUpload(bucket, object, uploadID string) error { 142 return c.AbortMultipartUploadWithContext(context.Background(), bucket, object, uploadID) 143 } 144 145 // GetBucketPolicy - fetches bucket access policy for a given bucket. 146 func (c Core) GetBucketPolicy(bucket string) (string, error) { 147 return c.getBucketPolicy(bucket) 148 } 149 150 // PutBucketPolicy - applies a new bucket access policy for a given bucket. 151 func (c Core) PutBucketPolicy(bucket, bucketPolicy string) error { 152 return c.PutBucketPolicyWithContext(context.Background(), bucket, bucketPolicy) 153 } 154 155 // PutBucketPolicyWithContext - applies a new bucket access policy for a given bucket with a context to control 156 // cancellations and timeouts. 157 func (c Core) PutBucketPolicyWithContext(ctx context.Context, bucket, bucketPolicy string) error { 158 return c.putBucketPolicy(ctx, bucket, bucketPolicy) 159 } 160 161 // GetObjectWithContext is a lower level API implemented to support reading 162 // partial objects and also downloading objects with special conditions 163 // matching etag, modtime etc. 164 func (c Core) GetObjectWithContext(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (io.ReadCloser, ObjectInfo, http.Header, error) { 165 return c.getObject(ctx, bucketName, objectName, opts) 166 } 167 168 // GetObject is a lower level API implemented to support reading 169 // partial objects and also downloading objects with special conditions 170 // matching etag, modtime etc. 171 func (c Core) GetObject(bucketName, objectName string, opts GetObjectOptions) (io.ReadCloser, ObjectInfo, http.Header, error) { 172 return c.GetObjectWithContext(context.Background(), bucketName, objectName, opts) 173 } 174 175 // StatObjectWithContext is a lower level API implemented to support special 176 // conditions matching etag, modtime on a request. 177 func (c Core) StatObjectWithContext(ctx context.Context, bucketName, objectName string, opts StatObjectOptions) (ObjectInfo, error) { 178 return c.statObject(ctx, bucketName, objectName, opts) 179 } 180 181 // StatObject is a lower level API implemented to support special 182 // conditions matching etag, modtime on a request. 183 func (c Core) StatObject(bucketName, objectName string, opts StatObjectOptions) (ObjectInfo, error) { 184 return c.StatObjectWithContext(context.Background(), bucketName, objectName, opts) 185 }