|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2015-2025 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 | + "bytes" |
| 22 | + "context" |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "net/http" |
| 27 | + "strconv" |
| 28 | + |
| 29 | + "github.com/minio/minio-go/v7/pkg/s3utils" |
| 30 | +) |
| 31 | + |
| 32 | +// AppendObjectOptions https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-buckets-objects-append.html |
| 33 | +type AppendObjectOptions struct { |
| 34 | + // Provide a progress reader to indicate the current append() progress. |
| 35 | + Progress io.Reader |
| 36 | + // ChunkSize indicates the maximum append() size, |
| 37 | + // it is useful when you want to control how much data |
| 38 | + // per append() you are interested in sending to server |
| 39 | + // while keeping the input io.Reader of a longer length. |
| 40 | + ChunkSize uint64 |
| 41 | + // Aggressively disable sha256 payload, it is automatically |
| 42 | + // turned-off for TLS supporting endpoints, useful in benchmarks |
| 43 | + // where you are interested in the peak() numbers. |
| 44 | + DisableContentSha256 bool |
| 45 | + |
| 46 | + customHeaders http.Header |
| 47 | + checksumType ChecksumType |
| 48 | +} |
| 49 | + |
| 50 | +// Header returns the custom header for AppendObject API |
| 51 | +func (opts AppendObjectOptions) Header() (header http.Header) { |
| 52 | + header = make(http.Header) |
| 53 | + for k, v := range opts.customHeaders { |
| 54 | + header[k] = v |
| 55 | + } |
| 56 | + return header |
| 57 | +} |
| 58 | + |
| 59 | +func (opts *AppendObjectOptions) setWriteOffset(offset int64) { |
| 60 | + if len(opts.customHeaders) == 0 { |
| 61 | + opts.customHeaders = make(http.Header) |
| 62 | + } |
| 63 | + opts.customHeaders["x-amz-write-offset-bytes"] = []string{strconv.FormatInt(offset, 10)} |
| 64 | +} |
| 65 | + |
| 66 | +func (opts *AppendObjectOptions) setChecksumParams(info ObjectInfo) { |
| 67 | + if len(opts.customHeaders) == 0 { |
| 68 | + opts.customHeaders = make(http.Header) |
| 69 | + } |
| 70 | + fullObject := info.ChecksumMode == ChecksumFullObjectMode.String() |
| 71 | + switch { |
| 72 | + case info.ChecksumCRC32 != "": |
| 73 | + if fullObject { |
| 74 | + opts.checksumType = ChecksumFullObjectCRC32 |
| 75 | + } |
| 76 | + case info.ChecksumCRC32C != "": |
| 77 | + if fullObject { |
| 78 | + opts.checksumType = ChecksumFullObjectCRC32C |
| 79 | + } |
| 80 | + case info.ChecksumCRC64NVME != "": |
| 81 | + // CRC64NVME only has a full object variant |
| 82 | + // so it does not carry any special full object |
| 83 | + // modifier |
| 84 | + opts.checksumType = ChecksumCRC64NVME |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (opts AppendObjectOptions) validate(c *Client) (err error) { |
| 89 | + if opts.ChunkSize > maxPartSize { |
| 90 | + return errInvalidArgument("Append chunkSize cannot be larger than max part size allowed") |
| 91 | + } |
| 92 | + switch { |
| 93 | + case !c.trailingHeaderSupport: |
| 94 | + return errInvalidArgument("AppendObject() requires Client with TrailingHeaders enabled") |
| 95 | + case c.overrideSignerType.IsV2(): |
| 96 | + return errInvalidArgument("AppendObject() cannot be used with v2 signatures") |
| 97 | + case s3utils.IsGoogleEndpoint(*c.endpointURL): |
| 98 | + return errInvalidArgument("AppendObject() cannot be used with GCS endpoints") |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// appendObjectDo - executes the append object http operation. |
| 105 | +// NOTE: You must have WRITE permissions on a bucket to add an object to it. |
| 106 | +func (c *Client) appendObjectDo(ctx context.Context, bucketName, objectName string, reader io.Reader, size int64, opts AppendObjectOptions) (UploadInfo, error) { |
| 107 | + // Input validation. |
| 108 | + if err := s3utils.CheckValidBucketName(bucketName); err != nil { |
| 109 | + return UploadInfo{}, err |
| 110 | + } |
| 111 | + if err := s3utils.CheckValidObjectName(objectName); err != nil { |
| 112 | + return UploadInfo{}, err |
| 113 | + } |
| 114 | + |
| 115 | + // Set headers. |
| 116 | + customHeader := opts.Header() |
| 117 | + |
| 118 | + // Populate request metadata. |
| 119 | + reqMetadata := requestMetadata{ |
| 120 | + bucketName: bucketName, |
| 121 | + objectName: objectName, |
| 122 | + customHeader: customHeader, |
| 123 | + contentBody: reader, |
| 124 | + contentLength: size, |
| 125 | + streamSha256: !opts.DisableContentSha256, |
| 126 | + } |
| 127 | + |
| 128 | + if opts.checksumType.IsSet() { |
| 129 | + reqMetadata.addCrc = &opts.checksumType |
| 130 | + } |
| 131 | + |
| 132 | + // Execute PUT an objectName. |
| 133 | + resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata) |
| 134 | + defer closeResponse(resp) |
| 135 | + if err != nil { |
| 136 | + return UploadInfo{}, err |
| 137 | + } |
| 138 | + if resp != nil { |
| 139 | + if resp.StatusCode != http.StatusOK { |
| 140 | + return UploadInfo{}, httpRespToErrorResponse(resp, bucketName, objectName) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + h := resp.Header |
| 145 | + |
| 146 | + // When AppendObject() is used, S3 Express will return final object size as x-amz-object-size |
| 147 | + if amzSize := h.Get("x-amz-object-size"); amzSize != "" { |
| 148 | + size, err = strconv.ParseInt(amzSize, 10, 64) |
| 149 | + if err != nil { |
| 150 | + return UploadInfo{}, err |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return UploadInfo{ |
| 155 | + Bucket: bucketName, |
| 156 | + Key: objectName, |
| 157 | + ETag: trimEtag(h.Get("ETag")), |
| 158 | + Size: size, |
| 159 | + |
| 160 | + // Checksum values |
| 161 | + ChecksumCRC32: h.Get(ChecksumCRC32.Key()), |
| 162 | + ChecksumCRC32C: h.Get(ChecksumCRC32C.Key()), |
| 163 | + ChecksumSHA1: h.Get(ChecksumSHA1.Key()), |
| 164 | + ChecksumSHA256: h.Get(ChecksumSHA256.Key()), |
| 165 | + ChecksumCRC64NVME: h.Get(ChecksumCRC64NVME.Key()), |
| 166 | + ChecksumMode: h.Get(ChecksumFullObjectMode.Key()), |
| 167 | + }, nil |
| 168 | +} |
| 169 | + |
| 170 | +// AppendObject - S3 Express Zone https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-buckets-objects-append.html |
| 171 | +func (c *Client) AppendObject(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, |
| 172 | + opts AppendObjectOptions, |
| 173 | +) (info UploadInfo, err error) { |
| 174 | + if objectSize < 0 && opts.ChunkSize == 0 { |
| 175 | + return UploadInfo{}, errors.New("object size must be provided when no chunk size is provided") |
| 176 | + } |
| 177 | + |
| 178 | + if err = opts.validate(c); err != nil { |
| 179 | + return UploadInfo{}, err |
| 180 | + } |
| 181 | + |
| 182 | + oinfo, err := c.StatObject(ctx, bucketName, objectName, StatObjectOptions{Checksum: true}) |
| 183 | + if err != nil { |
| 184 | + return UploadInfo{}, err |
| 185 | + } |
| 186 | + if oinfo.ChecksumMode != ChecksumFullObjectMode.String() { |
| 187 | + return UploadInfo{}, fmt.Errorf("append API is not allowed on objects that are not full_object checksum type: %s", oinfo.ChecksumMode) |
| 188 | + } |
| 189 | + opts.setChecksumParams(oinfo) // set the appropriate checksum params based on the existing object checksum metadata. |
| 190 | + opts.setWriteOffset(oinfo.Size) // First append must set the current object size as the offset. |
| 191 | + |
| 192 | + if opts.ChunkSize > 0 { |
| 193 | + finalObjSize := int64(-1) |
| 194 | + if objectSize > 0 { |
| 195 | + finalObjSize = info.Size + objectSize |
| 196 | + } |
| 197 | + totalPartsCount, partSize, lastPartSize, err := OptimalPartInfo(finalObjSize, opts.ChunkSize) |
| 198 | + if err != nil { |
| 199 | + return UploadInfo{}, err |
| 200 | + } |
| 201 | + buf := make([]byte, partSize) |
| 202 | + var partNumber int |
| 203 | + for partNumber = 1; partNumber <= totalPartsCount; partNumber++ { |
| 204 | + // Proceed to upload the part. |
| 205 | + if partNumber == totalPartsCount { |
| 206 | + partSize = lastPartSize |
| 207 | + } |
| 208 | + n, err := readFull(reader, buf) |
| 209 | + if err != nil { |
| 210 | + return info, err |
| 211 | + } |
| 212 | + if n != int(partSize) { |
| 213 | + return info, io.ErrUnexpectedEOF |
| 214 | + } |
| 215 | + rd := newHook(bytes.NewReader(buf[:n]), opts.Progress) |
| 216 | + uinfo, err := c.appendObjectDo(ctx, bucketName, objectName, rd, partSize, opts) |
| 217 | + if err != nil { |
| 218 | + return info, err |
| 219 | + } |
| 220 | + opts.setWriteOffset(uinfo.Size) |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + rd := newHook(reader, opts.Progress) |
| 225 | + return c.appendObjectDo(ctx, bucketName, objectName, rd, objectSize, opts) |
| 226 | +} |
0 commit comments