1
|
|
|
package s3 |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"strings" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
SDK "github.com/aws/aws-sdk-go-v2/service/s3" |
9
|
|
|
|
10
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors" |
11
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
// CopyObject executes `CopyObject` operation. |
15
|
|
|
func (svc *S3) CopyObject(ctx context.Context, r CopyObjectRequest) (*CopyObjectResult, error) { |
16
|
|
|
out, err := svc.RawCopyObject(ctx, r.ToInput()) |
17
|
|
|
if err != nil { |
18
|
|
|
err = svc.errWrap(errors.ErrorData{ |
19
|
|
|
Err: err, |
20
|
|
|
AWSOperation: "CopyObject", |
21
|
|
|
}) |
22
|
|
|
svc.Errorf(err.Error()) |
|
|
|
|
23
|
|
|
return nil, err |
24
|
|
|
} |
25
|
|
|
return NewCopyObjectResult(out), nil |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// CopyObjectRequest has parameters for `CopyObject` operation. |
29
|
|
|
type CopyObjectRequest struct { |
30
|
|
|
Bucket string |
31
|
|
|
Key string |
32
|
|
|
CopySource string |
33
|
|
|
|
34
|
|
|
// optional |
35
|
|
|
ACL ObjectCannedACL |
36
|
|
|
CacheControl string |
37
|
|
|
ContentDisposition string |
38
|
|
|
ContentEncoding string |
39
|
|
|
ContentLanguage string |
40
|
|
|
ContentType string |
41
|
|
|
CopySourceIfMatch string |
42
|
|
|
CopySourceIfModifiedSince time.Time |
43
|
|
|
CopySourceIfNoneMatch string |
44
|
|
|
CopySourceIfUnmodifiedSince time.Time |
45
|
|
|
CopySourceSSECustomerAlgorithm string |
46
|
|
|
CopySourceSSECustomerKey string |
47
|
|
|
CopySourceSSECustomerKeyMD5 string |
48
|
|
|
Expires time.Time |
49
|
|
|
GrantFullControl string |
50
|
|
|
GrantRead string |
51
|
|
|
GrantReadACP string |
52
|
|
|
GrantWriteACP string |
53
|
|
|
Metadata map[string]string |
54
|
|
|
MetadataDirective MetadataDirective |
55
|
|
|
ObjectLockLegalHoldStatus ObjectLockLegalHoldStatus |
56
|
|
|
ObjectLockMode ObjectLockMode |
57
|
|
|
ObjectLockRetainUntilDate time.Time |
58
|
|
|
RequestPayer RequestPayer |
59
|
|
|
SSECustomerAlgorithm string |
60
|
|
|
SSECustomerKey string |
61
|
|
|
SSECustomerKeyMD5 string |
62
|
|
|
SSEKMSEncryptionContext string |
63
|
|
|
SSEKMSKeyID string |
64
|
|
|
ServerSideEncryption ServerSideEncryption |
65
|
|
|
StorageClass StorageClass |
66
|
|
|
Tagging string |
67
|
|
|
TaggingDirective TaggingDirective |
68
|
|
|
WebsiteRedirectLocation string |
69
|
|
|
|
70
|
|
|
// TaggingTagSet is used for tagging. this won't override `Tagging` and can be used together. |
71
|
|
|
TaggingTagSet []Tag |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
func (r CopyObjectRequest) ToInput() *SDK.CopyObjectInput { |
|
|
|
|
75
|
|
|
in := &SDK.CopyObjectInput{} |
76
|
|
|
if r.Bucket != "" { |
77
|
|
|
in.Bucket = pointers.String(r.Bucket) |
78
|
|
|
} |
79
|
|
|
if r.Key != "" { |
80
|
|
|
in.Key = pointers.String(r.Key) |
81
|
|
|
} |
82
|
|
|
if r.CopySource != "" { |
83
|
|
|
in.CopySource = pointers.String(r.CopySource) |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
in.ACL = SDK.ObjectCannedACL(r.ACL) |
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.ContentType != "" { |
101
|
|
|
in.ContentType = pointers.String(r.ContentType) |
102
|
|
|
} |
103
|
|
|
if r.CopySourceIfMatch != "" { |
104
|
|
|
in.CopySourceIfMatch = pointers.String(r.CopySourceIfMatch) |
105
|
|
|
} |
106
|
|
|
if !r.CopySourceIfModifiedSince.IsZero() { |
107
|
|
|
in.CopySourceIfModifiedSince = &r.CopySourceIfModifiedSince |
108
|
|
|
} |
109
|
|
|
if r.CopySourceIfNoneMatch != "" { |
110
|
|
|
in.CopySourceIfNoneMatch = pointers.String(r.CopySourceIfNoneMatch) |
111
|
|
|
} |
112
|
|
|
if !r.CopySourceIfUnmodifiedSince.IsZero() { |
113
|
|
|
in.CopySourceIfUnmodifiedSince = &r.CopySourceIfUnmodifiedSince |
114
|
|
|
} |
115
|
|
|
if r.CopySourceSSECustomerAlgorithm != "" { |
116
|
|
|
in.CopySourceSSECustomerAlgorithm = pointers.String(r.CopySourceSSECustomerAlgorithm) |
117
|
|
|
} |
118
|
|
|
if r.CopySourceSSECustomerKey != "" { |
119
|
|
|
in.CopySourceSSECustomerKey = pointers.String(r.CopySourceSSECustomerKey) |
120
|
|
|
} |
121
|
|
|
if r.CopySourceSSECustomerKeyMD5 != "" { |
122
|
|
|
in.CopySourceSSECustomerKeyMD5 = pointers.String(r.CopySourceSSECustomerKeyMD5) |
123
|
|
|
} |
124
|
|
|
if !r.Expires.IsZero() { |
125
|
|
|
in.Expires = &r.Expires |
126
|
|
|
} |
127
|
|
|
if r.GrantFullControl != "" { |
128
|
|
|
in.GrantFullControl = pointers.String(r.GrantFullControl) |
129
|
|
|
} |
130
|
|
|
if r.GrantRead != "" { |
131
|
|
|
in.GrantRead = pointers.String(r.GrantRead) |
132
|
|
|
} |
133
|
|
|
if r.GrantReadACP != "" { |
134
|
|
|
in.GrantReadACP = pointers.String(r.GrantReadACP) |
135
|
|
|
} |
136
|
|
|
if r.GrantWriteACP != "" { |
137
|
|
|
in.GrantWriteACP = pointers.String(r.GrantWriteACP) |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
in.Metadata = r.Metadata |
141
|
|
|
in.MetadataDirective = SDK.MetadataDirective(r.MetadataDirective) |
142
|
|
|
in.ObjectLockLegalHoldStatus = SDK.ObjectLockLegalHoldStatus(r.ObjectLockLegalHoldStatus) |
143
|
|
|
in.ObjectLockMode = SDK.ObjectLockMode(r.ObjectLockMode) |
144
|
|
|
|
145
|
|
|
if !r.ObjectLockRetainUntilDate.IsZero() { |
146
|
|
|
in.ObjectLockRetainUntilDate = &r.ObjectLockRetainUntilDate |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
in.RequestPayer = SDK.RequestPayer(r.RequestPayer) |
150
|
|
|
|
151
|
|
|
if r.SSECustomerAlgorithm != "" { |
152
|
|
|
in.SSECustomerAlgorithm = pointers.String(r.SSECustomerAlgorithm) |
153
|
|
|
} |
154
|
|
|
if r.SSECustomerKey != "" { |
155
|
|
|
in.SSECustomerKey = pointers.String(r.SSECustomerKey) |
156
|
|
|
} |
157
|
|
|
if r.SSECustomerKeyMD5 != "" { |
158
|
|
|
in.SSECustomerKeyMD5 = pointers.String(r.SSECustomerKeyMD5) |
159
|
|
|
} |
160
|
|
|
if r.SSEKMSEncryptionContext != "" { |
161
|
|
|
in.SSEKMSEncryptionContext = pointers.String(r.SSEKMSEncryptionContext) |
162
|
|
|
} |
163
|
|
|
if r.SSEKMSKeyID != "" { |
164
|
|
|
in.SSEKMSKeyId = pointers.String(r.SSEKMSKeyID) |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
in.ServerSideEncryption = SDK.ServerSideEncryption(r.ServerSideEncryption) |
168
|
|
|
in.StorageClass = SDK.StorageClass(r.StorageClass) |
169
|
|
|
|
170
|
|
|
tags := r.Tagging |
171
|
|
|
if len(r.TaggingTagSet) != 0 { |
172
|
|
|
tt := make([]string, 0, len(r.TaggingTagSet)) |
173
|
|
|
if tags != "" { |
174
|
|
|
tt = append(tt, tags) |
175
|
|
|
} |
176
|
|
|
for _, v := range r.TaggingTagSet { |
177
|
|
|
tt = append(tt, v.Key+"="+v.Value) |
178
|
|
|
} |
179
|
|
|
tags = strings.Join(tt, "&") |
180
|
|
|
} |
181
|
|
|
if tags != "" { |
182
|
|
|
in.Tagging = pointers.String(tags) |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
in.TaggingDirective = SDK.TaggingDirective(r.TaggingDirective) |
186
|
|
|
|
187
|
|
|
if r.WebsiteRedirectLocation != "" { |
188
|
|
|
in.WebsiteRedirectLocation = pointers.String(r.WebsiteRedirectLocation) |
189
|
|
|
} |
190
|
|
|
return in |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// CopyObjectResult contains results from `CopyObject` operation. |
194
|
|
|
type CopyObjectResult struct { |
195
|
|
|
CopySourceVersionID string |
196
|
|
|
Expiration string |
197
|
|
|
RequestCharged RequestCharged |
198
|
|
|
SSECustomerAlgorithm string |
199
|
|
|
SSECustomerKeyMD5 string |
200
|
|
|
SSEKMSEncryptionContext string |
201
|
|
|
SSEKMSKeyID string |
202
|
|
|
ServerSideEncryption ServerSideEncryption |
203
|
|
|
VersionID string |
204
|
|
|
|
205
|
|
|
CopyResultETag string |
206
|
|
|
CopyResultLastModified time.Time |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
func NewCopyObjectResult(output *SDK.CopyObjectResponse) *CopyObjectResult { |
|
|
|
|
210
|
|
|
r := &CopyObjectResult{} |
211
|
|
|
if output == nil { |
212
|
|
|
return r |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if output.CopySourceVersionId != nil { |
216
|
|
|
r.CopySourceVersionID = *output.CopySourceVersionId |
217
|
|
|
} |
218
|
|
|
if output.Expiration != nil { |
219
|
|
|
r.Expiration = *output.Expiration |
220
|
|
|
} |
221
|
|
|
if output.RequestCharged != "" { |
222
|
|
|
r.RequestCharged = RequestCharged(output.RequestCharged) |
223
|
|
|
} |
224
|
|
|
if output.SSECustomerAlgorithm != nil { |
225
|
|
|
r.SSECustomerAlgorithm = *output.SSECustomerAlgorithm |
226
|
|
|
} |
227
|
|
|
if output.SSECustomerKeyMD5 != nil { |
228
|
|
|
r.SSECustomerKeyMD5 = *output.SSECustomerKeyMD5 |
229
|
|
|
} |
230
|
|
|
if output.SSEKMSEncryptionContext != nil { |
231
|
|
|
r.SSEKMSEncryptionContext = *output.SSEKMSEncryptionContext |
232
|
|
|
} |
233
|
|
|
if output.SSEKMSKeyId != nil { |
234
|
|
|
r.SSEKMSKeyID = *output.SSEKMSKeyId |
235
|
|
|
} |
236
|
|
|
if output.ServerSideEncryption != "" { |
237
|
|
|
r.ServerSideEncryption = ServerSideEncryption(output.ServerSideEncryption) |
238
|
|
|
} |
239
|
|
|
if output.VersionId != nil { |
240
|
|
|
r.VersionID = *output.VersionId |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if output.CopyObjectResult != nil { |
244
|
|
|
v := output.CopyObjectResult |
245
|
|
|
if v.ETag != nil { |
246
|
|
|
r.CopyResultETag = *v.ETag |
247
|
|
|
} |
248
|
|
|
if v.LastModified != nil { |
249
|
|
|
r.CopyResultLastModified = *v.LastModified |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
return r |
253
|
|
|
} |
254
|
|
|
|