PutObjectFanOut - is a variant of PutObject instead of writing a single object from a single stream multiple objects are written, defined via a list of PutObjectFanOutRequests. Each entry in PutObjectFanOutRequest carries an object keyname and its relevant metadata if any. `Key` is mandatory, rest o
(ctx context.Context, bucket string, fanOutData io.Reader, fanOutReq PutObjectFanOutRequest)
| 70 | // in PutObjectFanOutRequest carries an object keyname and its relevant metadata if any. `Key` is |
| 71 | // mandatory, rest of the other options in PutObjectFanOutRequest are optional. |
| 72 | func (c *Client) PutObjectFanOut(ctx context.Context, bucket string, fanOutData io.Reader, fanOutReq PutObjectFanOutRequest) ([]PutObjectFanOutResponse, error) { |
| 73 | if len(fanOutReq.Entries) == 0 { |
| 74 | return nil, errInvalidArgument("fan out requests cannot be empty") |
| 75 | } |
| 76 | |
| 77 | policy := NewPostPolicy() |
| 78 | policy.SetBucket(bucket) |
| 79 | policy.SetKey(strconv.FormatInt(time.Now().UnixNano(), 16)) |
| 80 | |
| 81 | // Expires in 15 minutes. |
| 82 | policy.SetExpires(time.Now().UTC().Add(15 * time.Minute)) |
| 83 | |
| 84 | // Set encryption headers if any. |
| 85 | policy.SetEncryption(fanOutReq.SSE) |
| 86 | |
| 87 | // Set checksum headers if any. |
| 88 | err := policy.SetChecksum(fanOutReq.Checksum) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | |
| 93 | url, formData, err := c.PresignedPostPolicy(ctx, policy) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | |
| 98 | r, w := io.Pipe() |
| 99 | |
| 100 | req, err := http.NewRequest(http.MethodPost, url.String(), r) |
| 101 | if err != nil { |
| 102 | w.Close() |
| 103 | return nil, err |
| 104 | } |
| 105 | |
| 106 | var b strings.Builder |
| 107 | enc := json.NewEncoder(&b) |
| 108 | for _, req := range fanOutReq.Entries { |
| 109 | if req.Key == "" { |
| 110 | w.Close() |
| 111 | return nil, errors.New("PutObjectFanOutRequest.Key is mandatory and cannot be empty") |
| 112 | } |
| 113 | if err = enc.Encode(&req); err != nil { |
| 114 | w.Close() |
| 115 | return nil, err |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | mwriter := multipart.NewWriter(w) |
| 120 | req.Header.Add("Content-Type", mwriter.FormDataContentType()) |
| 121 | |
| 122 | go func() { |
| 123 | defer w.Close() |
| 124 | defer mwriter.Close() |
| 125 | |
| 126 | for k, v := range formData { |
| 127 | if err := mwriter.WriteField(k, v); err != nil { |
| 128 | return |
| 129 | } |
no test coverage detected