/
post-policy-fan-out.go
122 lines (107 loc) · 3.46 KB
/
post-policy-fan-out.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2015-2023 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"bytes"
"context"
"sync"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/s3utils"
"github.com/minio/minio/internal/crypto"
"github.com/minio/minio/internal/hash"
xhttp "github.com/minio/minio/internal/http"
"github.com/minio/minio/internal/kms"
)
type fanOutOptions struct {
Kind crypto.Type
KeyID string
Key []byte
KmsCtx kms.Context
Checksum *hash.Checksum
MD5Hex string
}
// fanOutPutObject takes an input source reader and fans out multiple PUT operations
// based on the incoming fan-out request, a context cancellation by the caller
// would ensure all fan-out operations are canceled.
func fanOutPutObject(ctx context.Context, bucket string, objectAPI ObjectLayer, fanOutEntries []minio.PutObjectFanOutEntry, fanOutBuf []byte, opts fanOutOptions) ([]ObjectInfo, []error) {
errs := make([]error, len(fanOutEntries))
objInfos := make([]ObjectInfo, len(fanOutEntries))
var wg sync.WaitGroup
for i, req := range fanOutEntries {
wg.Add(1)
go func(idx int, req minio.PutObjectFanOutEntry) {
defer wg.Done()
objInfos[idx] = ObjectInfo{Name: req.Key}
hopts := hash.Options{
Size: int64(len(fanOutBuf)),
MD5Hex: opts.MD5Hex,
SHA256Hex: "",
ActualSize: -1,
DisableMD5: true,
}
hr, err := hash.NewReaderWithOpts(ctx, bytes.NewReader(fanOutBuf), hopts)
if err != nil {
errs[idx] = err
return
}
reader := NewPutObjReader(hr)
defer func() {
if err := reader.Close(); err != nil {
errs[idx] = err
}
if err := hr.Close(); err != nil {
errs[idx] = err
}
}()
userDefined := make(map[string]string, len(req.UserMetadata))
for k, v := range req.UserMetadata {
userDefined[k] = v
}
userDefined[xhttp.AmzObjectTagging] = s3utils.TagEncode(req.UserTags)
if opts.Kind != nil {
encrd, objectEncryptionKey, err := newEncryptReader(ctx, hr, opts.Kind, opts.KeyID, opts.Key, bucket, req.Key, userDefined, opts.KmsCtx)
if err != nil {
errs[idx] = err
return
}
// do not try to verify encrypted content/
hr, err = hash.NewReader(ctx, encrd, -1, "", "", -1)
if err != nil {
errs[idx] = err
return
}
reader, err = reader.WithEncryption(hr, &objectEncryptionKey)
if err != nil {
errs[idx] = err
return
}
}
objInfo, err := objectAPI.PutObject(ctx, bucket, req.Key, reader, ObjectOptions{
Versioned: globalBucketVersioningSys.PrefixEnabled(bucket, req.Key),
VersionSuspended: globalBucketVersioningSys.PrefixSuspended(bucket, req.Key),
UserDefined: userDefined,
})
if err != nil {
errs[idx] = err
return
}
objInfos[idx] = objInfo
}(i, req)
}
wg.Wait()
return objInfos, errs
}