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