Skip to content

Commit f62389a

Browse files
implement fan-out API (#1826)
1 parent 78f1dd8 commit f62389a

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

api-put-object-fan-out.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}

docs/API.md

+40
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,46 @@ if err != nil {
504504
}
505505
```
506506

507+
<a name="PutObjectFanOut"></a>
508+
### PutObjectFanOut(ctx context.Context, bucket string, body io.Reader, fanOutReq ...PutObjectFanOutRequest) ([]PutObjectFanOutResponse, error)
509+
A variant of PutObject instead of writing a single object from a single stream multiple objects are written, defined via a list of
510+
*PutObjectFanOutRequest*. Each entry in *PutObjectFanOutRequest* carries an object keyname and its relevant metadata if any.
511+
`Key` is mandatory, rest of the other options in *PutObjectFanOutRequest( are optional.
512+
513+
__Parameters__
514+
515+
| Param | Type | Description |
516+
|:-------------|:---------------------------------|:----------------------------------------------------------------------|
517+
| `ctx` | _context.Context_ | Custom context for timeout/cancellation of the call |
518+
| `bucketName` | _string_ | Name of the bucket |
519+
| `body` | _io.Reader_ | Any Go type that implements io.Reader |
520+
| `fanOutReq` | _[]minio.PutObjectFanOutRequest_ | User input list of all the objects that will be created on the server |
521+
522+
__minio.PutObjectFanOutRequest__
523+
524+
| Field | Type | Description |
525+
|:---------------------|:----------------------|:---------------------------------------------------------------------------------------------------|
526+
| `Key` | _string_ | Name of the object |
527+
| `UserMetadata` | _map[string]string_ | Map of user metadata |
528+
| `UserTags` | _map[string]string_ | Map of user object tags |
529+
| `ContentType` | _string_ | Content type of object, e.g "application/text" |
530+
| `ContentEncoding` | _string_ | Content encoding of object, e.g "gzip" |
531+
| `ContentDisposition` | _string_ | Content disposition of object, "inline" |
532+
| `ContentLanguage` | _string_ | Content language of object, e.g "French" |
533+
| `CacheControl` | _string_ | Used to specify directives for caching mechanisms in both requests and responses e.g "max-age=600" |
534+
| `Retention` | _minio.RetentionMode_ | Retention mode to be set, e.g "COMPLIANCE" |
535+
| `RetainUntilDate` | _time.Time_ | Time until which the retention applied is valid |
536+
537+
__minio.PutObjectFanOutResponse__
538+
539+
| Field | Type | Description |
540+
|:---------------|:-----------|:----------------------------------------------------------------|
541+
| `Key` | _string_ | Name of the object |
542+
| `ETag` | _string_ | ETag opaque unique value of the object |
543+
| `VersionID` | _string_ | VersionID of the uploaded object |
544+
| `LastModified` | _time.Time | Last modified time of the latest object |
545+
| `Error` | _error_ | Is non `nil` only when the fan-out for a specific object failed |
546+
507547
<a name="PutObject"></a>
508548
### PutObject(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64,opts PutObjectOptions) (info UploadInfo, err error)
509549
Uploads objects that are less than 128MiB in a single PUT operation. For objects that are greater than 128MiB in size, PutObject seamlessly uploads the object as parts of 128MiB or more depending on the actual file size. The max upload size for an object is 5TB.

examples/minio/put-object-fan-out.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//go:build example
2+
// +build example
3+
4+
/*
5+
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
6+
* Copyright 2023 MinIO, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package main
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"log"
27+
"os"
28+
29+
"github.com/minio/minio-go/v7"
30+
"github.com/minio/minio-go/v7/pkg/credentials"
31+
)
32+
33+
func main() {
34+
const (
35+
// Note: These constants are dummy values,
36+
// please replace them with values for your setup.
37+
YOURACCESSKEYID = "Q3AM3UQ867SPQQA43P2F"
38+
YOURSECRETACCESSKEY = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
39+
YOURENDPOINT = "play.min.io"
40+
YOURBUCKET = "mybucket" // 'mc mb play/mybucket' if it does not exist.
41+
)
42+
43+
// Requests are always secure (HTTPS) by default. Set secure=false to enable insecure (HTTP) access.
44+
// This boolean value is the last argument for New().
45+
46+
// New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically
47+
// determined based on the Endpoint value.
48+
minioClient, err := minio.New(YOURENDPOINT, &minio.Options{
49+
Creds: credentials.NewStaticV4(YOURACCESSKEYID, YOURSECRETACCESSKEY, ""),
50+
Secure: true,
51+
})
52+
if err != nil {
53+
log.Fatalln(err)
54+
}
55+
56+
filePath := "my-testfile" // Specify a local file that we will upload
57+
58+
// Open a local file that we will upload
59+
file, err := os.Open(filePath)
60+
if err != nil {
61+
log.Fatalln(err)
62+
}
63+
defer file.Close()
64+
65+
fanOutReq := []minio.PutObjectFanOutRequest{
66+
minio.PutObjectFanOutRequest{Key: "my1-prefix/1.txt"},
67+
minio.PutObjectFanOutRequest{Key: "my1-prefix/2.txt"},
68+
minio.PutObjectFanOutRequest{Key: "my1-prefix/3.txt"},
69+
minio.PutObjectFanOutRequest{Key: "my1-prefix/4.txt"},
70+
minio.PutObjectFanOutRequest{Key: "my1-prefix/5.txt"},
71+
minio.PutObjectFanOutRequest{Key: "my1-prefix/6.txt"},
72+
}
73+
74+
fanOutResp, err := minioClient.PutObjectFanOut(context.Background(), "testbucket", file, fanOutReq...)
75+
if err != nil {
76+
log.Fatalln(err)
77+
}
78+
79+
for _, resp := range fanOutResp {
80+
fmt.Println(resp)
81+
}
82+
}

0 commit comments

Comments
 (0)