|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2023 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 | + "encoding/json" |
| 23 | + "errors" |
| 24 | + "io" |
| 25 | + "mime/multipart" |
| 26 | + "net/http" |
| 27 | + "strconv" |
| 28 | + "strings" |
| 29 | + "time" |
| 30 | +) |
| 31 | + |
| 32 | +// PutObjectFanOutRequest this is the request structure sent |
| 33 | +// to the server to fan-out the stream to multiple objects. |
| 34 | +type PutObjectFanOutRequest struct { |
| 35 | + Key string `json:"key"` |
| 36 | + UserMetadata map[string]string `json:"metadata,omitempty"` |
| 37 | + UserTags map[string]string `json:"tags,omitempty"` |
| 38 | + ContentType string `json:"contentType,omitempty"` |
| 39 | + ContentEncoding string `json:"contentEncoding,omitempty"` |
| 40 | + ContentDisposition string `json:"contentDisposition,omitempty"` |
| 41 | + ContentLanguage string `json:"contentLanguage,omitempty"` |
| 42 | + CacheControl string `json:"cacheControl,omitempty"` |
| 43 | + Retention RetentionMode `json:"retention,omitempty"` |
| 44 | + RetainUntilDate *time.Time `json:"retainUntil,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// PutObjectFanOutResponse this is the response structure sent |
| 48 | +// by the server upon success or failure for each object |
| 49 | +// fan-out keys. Additionally this response carries ETag, |
| 50 | +// VersionID and LastModified for each object fan-out. |
| 51 | +type PutObjectFanOutResponse struct { |
| 52 | + Key string `json:"key"` |
| 53 | + ETag string `json:"etag,omitempty"` |
| 54 | + VersionID string `json:"versionId,omitempty"` |
| 55 | + LastModified *time.Time `json:"lastModified,omitempty"` |
| 56 | + Error error `json:"error,omitempty"` |
| 57 | +} |
| 58 | + |
| 59 | +// PutObjectFanOut - is a variant of PutObject instead of writing a single object from a single |
| 60 | +// stream multiple objects are written, defined via a list of PutObjectFanOutRequests. Each entry |
| 61 | +// in PutObjectFanOutRequest carries an object keyname and its relevant metadata if any. `Key` is |
| 62 | +// mandatory, rest of the other options in PutObjectFanOutRequest are optional. |
| 63 | +func (c *Client) PutObjectFanOut(ctx context.Context, bucket string, body io.Reader, fanOutReq ...PutObjectFanOutRequest) ([]PutObjectFanOutResponse, error) { |
| 64 | + if len(fanOutReq) == 0 { |
| 65 | + return nil, errInvalidArgument("fan out requests cannot be empty") |
| 66 | + } |
| 67 | + |
| 68 | + policy := NewPostPolicy() |
| 69 | + policy.SetBucket(bucket) |
| 70 | + policy.SetKey(strconv.FormatInt(time.Now().UnixNano(), 16)) |
| 71 | + |
| 72 | + // Expires in 15 minutes. |
| 73 | + policy.SetExpires(time.Now().UTC().Add(15 * time.Minute)) |
| 74 | + |
| 75 | + url, formData, err := c.PresignedPostPolicy(ctx, policy) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + r, w := io.Pipe() |
| 81 | + |
| 82 | + req, err := http.NewRequest(http.MethodPost, url.String(), r) |
| 83 | + if err != nil { |
| 84 | + w.Close() |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + var b strings.Builder |
| 89 | + enc := json.NewEncoder(&b) |
| 90 | + for _, req := range fanOutReq { |
| 91 | + if req.Key == "" { |
| 92 | + w.Close() |
| 93 | + return nil, errors.New("PutObjectFanOutRequest.Key is mandatory and cannot be empty") |
| 94 | + } |
| 95 | + if err = enc.Encode(&req); err != nil { |
| 96 | + w.Close() |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + mwriter := multipart.NewWriter(w) |
| 102 | + req.Header.Add("Content-Type", mwriter.FormDataContentType()) |
| 103 | + |
| 104 | + go func() { |
| 105 | + defer w.Close() |
| 106 | + defer mwriter.Close() |
| 107 | + |
| 108 | + for k, v := range formData { |
| 109 | + if err := mwriter.WriteField(k, v); err != nil { |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if err := mwriter.WriteField("x-minio-fanout-list", b.String()); err != nil { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + mw, err := mwriter.CreateFormFile("file", "fanout-content") |
| 119 | + if err != nil { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if _, err = io.Copy(mw, body); err != nil { |
| 124 | + return |
| 125 | + } |
| 126 | + }() |
| 127 | + |
| 128 | + resp, err := c.do(req) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + defer closeResponse(resp) |
| 133 | + |
| 134 | + if resp.StatusCode != http.StatusOK { |
| 135 | + return nil, httpRespToErrorResponse(resp, bucket, "fanout-content") |
| 136 | + } |
| 137 | + |
| 138 | + dec := json.NewDecoder(resp.Body) |
| 139 | + fanOutResp := make([]PutObjectFanOutResponse, 0, len(fanOutReq)) |
| 140 | + for dec.More() { |
| 141 | + var m PutObjectFanOutResponse |
| 142 | + if err = dec.Decode(&m); err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + fanOutResp = append(fanOutResp, m) |
| 146 | + } |
| 147 | + |
| 148 | + return fanOutResp, nil |
| 149 | +} |
0 commit comments