1
|
|
|
package s3 |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"context" |
6
|
|
|
"io" |
7
|
|
|
"strings" |
8
|
|
|
"time" |
9
|
|
|
|
10
|
|
|
SDK "github.com/aws/aws-sdk-go-v2/service/s3" |
11
|
|
|
|
12
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors" |
13
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
// PutObject executes `PutObject` operation. |
17
|
|
|
func (svc *S3) PutObject(ctx context.Context, r PutObjectRequest) (*PutObjectResult, error) { |
18
|
|
|
out, err := svc.RawPutObject(ctx, r.ToInput()) |
19
|
|
|
if err != nil { |
20
|
|
|
err = svc.errWrap(errors.ErrorData{ |
21
|
|
|
Err: err, |
22
|
|
|
AWSOperation: "PutObject", |
23
|
|
|
}) |
24
|
|
|
svc.Errorf(err.Error()) |
|
|
|
|
25
|
|
|
return nil, err |
26
|
|
|
} |
27
|
|
|
return NewPutObjectResult(out), nil |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// PutObjectRequest has parameters for `PutObject` operation. |
31
|
|
|
type PutObjectRequest struct { |
32
|
|
|
Bucket string |
33
|
|
|
Key string |
34
|
|
|
|
35
|
|
|
// optional |
36
|
|
|
ACL ObjectCannedACL |
37
|
|
|
Body io.ReadSeeker |
38
|
|
|
BodyBytes []byte |
39
|
|
|
CacheControl string |
40
|
|
|
ContentDisposition string |
41
|
|
|
ContentEncoding string |
42
|
|
|
ContentLanguage string |
43
|
|
|
ContentLength int64 |
44
|
|
|
ContentMD5 string |
45
|
|
|
ContentType string |
46
|
|
|
Expires time.Time |
47
|
|
|
GrantFullControl string |
48
|
|
|
GrantRead string |
49
|
|
|
GrantReadACP string |
50
|
|
|
GrantWriteACP string |
51
|
|
|
Metadata map[string]string |
52
|
|
|
ObjectLockLegalHoldStatus ObjectLockLegalHoldStatus |
53
|
|
|
ObjectLockMode ObjectLockMode |
54
|
|
|
ObjectLockRetainUntilDate time.Time |
55
|
|
|
RequestPayer RequestPayer |
56
|
|
|
SSECustomerAlgorithm string |
57
|
|
|
SSECustomerKey string |
58
|
|
|
SSECustomerKeyMD5 string |
59
|
|
|
SSEKMSEncryptionContext string |
60
|
|
|
SSEKMSKeyID string |
61
|
|
|
ServerSideEncryption ServerSideEncryption |
62
|
|
|
StorageClass StorageClass |
63
|
|
|
Tagging string |
64
|
|
|
WebsiteRedirectLocation string |
65
|
|
|
|
66
|
|
|
// TaggingTagSet is used for tagging. this won't override `Tagging` and can be used together. |
67
|
|
|
TaggingTagSet []Tag |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
func (r PutObjectRequest) ToInput() *SDK.PutObjectInput { |
|
|
|
|
71
|
|
|
in := &SDK.PutObjectInput{} |
72
|
|
|
if r.Bucket != "" { |
73
|
|
|
in.Bucket = pointers.String(r.Bucket) |
74
|
|
|
} |
75
|
|
|
if r.Key != "" { |
76
|
|
|
in.Key = pointers.String(r.Key) |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
in.ACL = SDK.ObjectCannedACL(r.ACL) |
80
|
|
|
|
81
|
|
|
switch { |
82
|
|
|
case r.Body != nil: |
83
|
|
|
in.Body = r.Body |
84
|
|
|
case len(r.BodyBytes) != 0: |
85
|
|
|
in.Body = bytes.NewReader(r.BodyBytes) |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if r.CacheControl != "" { |
89
|
|
|
in.CacheControl = pointers.String(r.CacheControl) |
90
|
|
|
} |
91
|
|
|
if r.ContentDisposition != "" { |
92
|
|
|
in.ContentDisposition = pointers.String(r.ContentDisposition) |
93
|
|
|
} |
94
|
|
|
if r.ContentEncoding != "" { |
95
|
|
|
in.ContentEncoding = pointers.String(r.ContentEncoding) |
96
|
|
|
} |
97
|
|
|
if r.ContentLanguage != "" { |
98
|
|
|
in.ContentLanguage = pointers.String(r.ContentLanguage) |
99
|
|
|
} |
100
|
|
|
if r.ContentLength != 0 { |
101
|
|
|
in.ContentLength = pointers.Long64(r.ContentLength) |
102
|
|
|
} |
103
|
|
|
if r.ContentMD5 != "" { |
104
|
|
|
in.ContentMD5 = pointers.String(r.ContentMD5) |
105
|
|
|
} |
106
|
|
|
if r.ContentType != "" { |
107
|
|
|
in.ContentType = pointers.String(r.ContentType) |
108
|
|
|
} |
109
|
|
|
if !r.Expires.IsZero() { |
110
|
|
|
in.Expires = &r.Expires |
111
|
|
|
} |
112
|
|
|
if r.GrantFullControl != "" { |
113
|
|
|
in.GrantFullControl = pointers.String(r.GrantFullControl) |
114
|
|
|
} |
115
|
|
|
if r.GrantRead != "" { |
116
|
|
|
in.GrantRead = pointers.String(r.GrantRead) |
117
|
|
|
} |
118
|
|
|
if r.GrantReadACP != "" { |
119
|
|
|
in.GrantReadACP = pointers.String(r.GrantReadACP) |
120
|
|
|
} |
121
|
|
|
if r.GrantWriteACP != "" { |
122
|
|
|
in.GrantWriteACP = pointers.String(r.GrantWriteACP) |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
in.Metadata = r.Metadata |
126
|
|
|
in.ObjectLockLegalHoldStatus = SDK.ObjectLockLegalHoldStatus(r.ObjectLockLegalHoldStatus) |
127
|
|
|
in.ObjectLockMode = SDK.ObjectLockMode(r.ObjectLockMode) |
128
|
|
|
|
129
|
|
|
if !r.ObjectLockRetainUntilDate.IsZero() { |
130
|
|
|
in.ObjectLockRetainUntilDate = &r.ObjectLockRetainUntilDate |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
in.RequestPayer = SDK.RequestPayer(r.RequestPayer) |
134
|
|
|
|
135
|
|
|
if r.SSECustomerAlgorithm != "" { |
136
|
|
|
in.SSECustomerAlgorithm = pointers.String(r.SSECustomerAlgorithm) |
137
|
|
|
} |
138
|
|
|
if r.SSECustomerKey != "" { |
139
|
|
|
in.SSECustomerKey = pointers.String(r.SSECustomerKey) |
140
|
|
|
} |
141
|
|
|
if r.SSECustomerKeyMD5 != "" { |
142
|
|
|
in.SSECustomerKeyMD5 = pointers.String(r.SSECustomerKeyMD5) |
143
|
|
|
} |
144
|
|
|
if r.SSEKMSEncryptionContext != "" { |
145
|
|
|
in.SSEKMSEncryptionContext = pointers.String(r.SSEKMSEncryptionContext) |
146
|
|
|
} |
147
|
|
|
if r.SSEKMSKeyID != "" { |
148
|
|
|
in.SSEKMSKeyId = pointers.String(r.SSEKMSKeyID) |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
in.ServerSideEncryption = SDK.ServerSideEncryption(r.ServerSideEncryption) |
152
|
|
|
in.StorageClass = SDK.StorageClass(r.StorageClass) |
153
|
|
|
|
154
|
|
|
tags := r.Tagging |
155
|
|
|
if len(r.TaggingTagSet) != 0 { |
156
|
|
|
tt := make([]string, 0, len(r.TaggingTagSet)) |
157
|
|
|
if tags != "" { |
158
|
|
|
tt = append(tt, tags) |
159
|
|
|
} |
160
|
|
|
for _, v := range r.TaggingTagSet { |
161
|
|
|
tt = append(tt, v.Key+"="+v.Value) |
162
|
|
|
} |
163
|
|
|
tags = strings.Join(tt, "&") |
164
|
|
|
} |
165
|
|
|
if tags != "" { |
166
|
|
|
in.Tagging = pointers.String(tags) |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if r.WebsiteRedirectLocation != "" { |
170
|
|
|
in.WebsiteRedirectLocation = pointers.String(r.WebsiteRedirectLocation) |
171
|
|
|
} |
172
|
|
|
return in |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// PutObjectResult contains results from `PutObject` operation. |
176
|
|
|
type PutObjectResult struct { |
177
|
|
|
ETag string |
178
|
|
|
Expiration string |
179
|
|
|
RequestCharged RequestCharged |
180
|
|
|
SSECustomerAlgorithm string |
181
|
|
|
SSECustomerKeyMD5 string |
182
|
|
|
SSEKMSEncryptionContext string |
183
|
|
|
SSEKMSKeyID string |
184
|
|
|
ServerSideEncryption ServerSideEncryption |
185
|
|
|
VersionID string |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
func NewPutObjectResult(output *SDK.PutObjectResponse) *PutObjectResult { |
|
|
|
|
189
|
|
|
r := &PutObjectResult{} |
190
|
|
|
if output == nil { |
191
|
|
|
return r |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if output.ETag != nil { |
195
|
|
|
r.ETag = *output.ETag |
196
|
|
|
} |
197
|
|
|
if output.Expiration != nil { |
198
|
|
|
r.Expiration = *output.Expiration |
199
|
|
|
} |
200
|
|
|
if output.RequestCharged != "" { |
201
|
|
|
r.RequestCharged = RequestCharged(output.RequestCharged) |
202
|
|
|
} |
203
|
|
|
if output.SSECustomerAlgorithm != nil { |
204
|
|
|
r.SSECustomerAlgorithm = *output.SSECustomerAlgorithm |
205
|
|
|
} |
206
|
|
|
if output.SSECustomerKeyMD5 != nil { |
207
|
|
|
r.SSECustomerKeyMD5 = *output.SSECustomerKeyMD5 |
208
|
|
|
} |
209
|
|
|
if output.SSEKMSEncryptionContext != nil { |
210
|
|
|
r.SSEKMSEncryptionContext = *output.SSEKMSEncryptionContext |
211
|
|
|
} |
212
|
|
|
if output.SSEKMSKeyId != nil { |
213
|
|
|
r.SSEKMSKeyID = *output.SSEKMSKeyId |
214
|
|
|
} |
215
|
|
|
if output.ServerSideEncryption != "" { |
216
|
|
|
r.ServerSideEncryption = ServerSideEncryption(output.ServerSideEncryption) |
217
|
|
|
} |
218
|
|
|
if output.VersionId != nil { |
219
|
|
|
r.VersionID = *output.VersionId |
220
|
|
|
} |
221
|
|
|
return r |
222
|
|
|
} |
223
|
|
|
|