-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_reporting.go
376 lines (320 loc) · 10.2 KB
/
object_reporting.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// ObjectReporting representins a JSON serializable representation of an
// S3UploadState record.
type ObjectReporting struct {
Bucket string
Key string
UploadId string `json:",omitempty"`
Completed bool
Aborted bool
FullChecksums *ObjectChecksums `json:",omitempty"`
ObjectChecksum *ObjectChecksums `json:",omitempty"`
ObjectAttributes *ObjectAttributes `json:",omitempty"`
Errors *ObjectErrors `json:",omitempty"`
}
func NewObjectReporting(st *S3UploadState) (*ObjectReporting, error) {
isPutObject := (st.obj != nil && st.objOutput != nil)
isMultipartObject := (st.create != nil && st.createOutput != nil)
isCompleted := ((isPutObject && st.objError == nil) ||
(st.create != nil && st.createOutput != nil && st.completedOutput != nil && st.completedError == nil))
isAborted := ((isPutObject && st.objError != nil) ||
(st.create != nil && st.createOutput != nil && st.abortedOutput != nil && st.abortedError == nil))
var Bucket string
var Key string
var uploadID string
if isPutObject {
Bucket = *st.obj.Bucket
Key = *st.obj.Key
} else if isMultipartObject {
Bucket = *st.create.Bucket
Key = *st.create.Key
if !(isCompleted || isAborted) {
uploadID = *st.createOutput.UploadId
}
} else {
err := fmt.Errorf("invalid S3UploadState: it is not a PutObject nor a MultipartObject")
return nil, err
}
var fullChecksums *ObjectChecksums
var objChecksums *ObjectChecksums
var objAttributes *ObjectAttributes
var err error
if isCompleted {
fullChecksums, err = NewObjectChecksums(st.hr)
if err != nil {
return nil, err
}
if st.hr.Count() == 1 {
objChecksums = AWSObjectChecksums(
st.hr.ChecksumAlgorithm(), st.hr.Sum())
} else {
objChecksums = AWSObjectChecksums(
st.hr.ChecksumAlgorithm(), st.hr.SumOfSums())
}
objAttributes, err = NewObjectAttributes(st.hr, st.objectAttributesOutput)
if err != nil {
return nil, err
}
}
var partErrors []*UploadPartError
if isMultipartObject {
for i, e := range st.uploadPartErrors {
if e == nil {
continue
}
partErrors = append(partErrors, &UploadPartError{
PartNumber: i,
Error: errorString(e),
})
}
}
errors := &ObjectErrors{
PutObjectError: errorString(st.objError),
UploadPartErrors: partErrors,
CompleteMultipartUploadError: errorString(st.completedError),
AbortMultipartUploadError: errorString(st.abortedError),
GetObjectAttributesError: errorString(st.objectAttributesError),
}
if len(errors.PutObjectError) == 0 &&
len(errors.UploadPartErrors) == 0 &&
len(errors.CompleteMultipartUploadError) == 0 &&
len(errors.AbortMultipartUploadError) == 0 &&
len(errors.GetObjectAttributesError) == 0 {
errors = nil
}
return &ObjectReporting{
Bucket: Bucket,
Key: Key,
UploadId: uploadID,
Completed: isCompleted,
Aborted: isAborted,
FullChecksums: fullChecksums,
ObjectChecksum: objChecksums,
ObjectAttributes: objAttributes,
Errors: errors,
}, nil
}
// ObjectChecksum provides human-readable representations of a HashSum checksum.
type ObjectChecksum struct {
Hex string
Base64 string
}
func NewObjectChecksum(sum HashSum) *ObjectChecksum {
p := &ObjectChecksum{
Hex: sum.Hex(),
Base64: sum.Base64(),
}
return p
}
// ObjectChecksums represents one or more nested ObjectChecksum.
type ObjectChecksums struct {
ChecksumMD5 *ObjectChecksum `json:"ChecksumMD5,omitempty"`
ChecksumCRC32 *ObjectChecksum `json:"ChecksumCRC32,omitempty"`
ChecksumCRC32C *ObjectChecksum `json:"ChecksumCRC32C,omitempty"`
ChecksumSHA1 *ObjectChecksum `json:"ChecksumSHA1,omitempty"`
ChecksumSHA256 *ObjectChecksum `json:"ChecksumSHA256,omitempty"`
}
// AWSObjectChecksums returns an ObjectChecksums for a specified algorithm and
// HashSum checksum.
func AWSObjectChecksums(algo *ChecksumAlgorithm, sum HashSum) *ObjectChecksums {
p := &ObjectChecksums{}
switch algo {
case ChecksumAlgorithmCRC32:
p.ChecksumCRC32 = NewObjectChecksum(sum)
case ChecksumAlgorithmCRC32C:
p.ChecksumCRC32C = NewObjectChecksum(sum)
case ChecksumAlgorithmSHA1:
p.ChecksumSHA1 = NewObjectChecksum(sum)
case ChecksumAlgorithmSHA256:
p.ChecksumSHA256 = NewObjectChecksum(sum)
}
return p
}
// NewObjectChecksums accepts either *S3Hasher or *types.Checksum and converts
// it into an *ObjectChecksum. If passed nil then nil will be returned.
func NewObjectChecksums(t interface{}) (*ObjectChecksums, error) {
var md5sum []byte
var sum HashSum
var algo *ChecksumAlgorithm
var err error
if t == nil {
return nil, nil
} else if hr, ok := t.(*S3Hasher); ok {
algo = hr.ChecksumAlgorithm()
sum = hr.Sum()
md5sum = hr.MD5Sum()
} else if x, ok := t.(*types.Checksum); ok {
var b64 HashSumBase64
p := &b64
if x.ChecksumCRC32 != nil {
algo = ChecksumAlgorithmCRC32
err = p.UnmarshalText([]byte(*x.ChecksumCRC32))
} else if x.ChecksumCRC32C != nil {
algo = ChecksumAlgorithmCRC32C
err = p.UnmarshalText([]byte(*x.ChecksumCRC32C))
} else if x.ChecksumSHA1 != nil {
algo = ChecksumAlgorithmSHA1
err = p.UnmarshalText([]byte(*x.ChecksumSHA1))
} else if x.ChecksumSHA256 != nil {
algo = ChecksumAlgorithmSHA256
err = p.UnmarshalText([]byte(*x.ChecksumSHA256))
} else {
err = fmt.Errorf("unknown types.Checksum: %#v", x)
}
if err != nil {
err = fmt.Errorf("error decoding %#v: %w", x, err)
return nil, err
} else {
sum = p.HashSum
}
} else {
err := fmt.Errorf("unhandled type passed to NewObjectChecksums: %t", t)
return nil, err
}
p := &ObjectChecksums{}
if md5sum != nil {
p.ChecksumMD5 = NewObjectChecksum(md5sum)
}
switch algo {
case ChecksumAlgorithmCRC32:
p.ChecksumCRC32 = NewObjectChecksum(sum)
case ChecksumAlgorithmCRC32C:
p.ChecksumCRC32C = NewObjectChecksum(sum)
case ChecksumAlgorithmSHA1:
p.ChecksumSHA1 = NewObjectChecksum(sum)
case ChecksumAlgorithmSHA256:
p.ChecksumSHA256 = NewObjectChecksum(sum)
}
return p, nil
}
// ObjectAttributes represents the available fields in
// s3.GetObjectAttributesOutput.
type ObjectAttributes struct {
DeleteMarker *bool `json:",omitempty"`
VersionId *string `json:",omitempty"`
LastModified *time.Time `json:",omitempty"`
ETag *string `json:",omitempty"`
Checksum *ObjectChecksums `json:",omitempty"`
ObjectParts *ObjectPartAttributes `json:",omitempty"`
}
func NewObjectAttributes(hr *S3Hasher, p *s3.GetObjectAttributesOutput) (*ObjectAttributes, error) {
checksum, err := NewObjectChecksums(p.Checksum)
if err != nil {
return nil, err
}
return &ObjectAttributes{
DeleteMarker: p.DeleteMarker,
VersionId: p.VersionId,
LastModified: p.LastModified,
ETag: p.ETag,
Checksum: checksum,
ObjectParts: NewObjectPartAttributes(hr, p.ObjectParts),
}, nil
}
// ObjectPartAttributes represents the available fields in
// types.GetObjectAttributeParts.
type ObjectPartAttributes struct {
IsTruncated *bool `json:",omitempty"`
TotalPartsCount *int32 `json:",omitempty"`
Parts []*ObjectPart `json:",omitempty"`
}
func NewObjectPartAttributes(hr *S3Hasher, p *types.GetObjectAttributesParts) *ObjectPartAttributes {
if p == nil {
return nil
}
return &ObjectPartAttributes{
IsTruncated: p.IsTruncated,
TotalPartsCount: p.TotalPartsCount,
Parts: NewObjectParts(hr, p.Parts),
}
}
// NewObjectParts reprsents one or more types.ObjectPart, if the parts slice is
// empty or nil then nil is returned.
func NewObjectParts(hr *S3Hasher, parts []types.ObjectPart) []*ObjectPart {
if len(parts) == 0 {
return nil
}
var op []*ObjectPart
checksumObject := func(s *string) *ObjectChecksum {
var sum HashSumBase64
if s == nil {
return nil
}
err := (&sum).UnmarshalText([]byte(*s))
if err != nil {
panic(fmt.Sprintf("invalid base64 checksum returned by AWS! %s: %s", *s, err))
}
return NewObjectChecksum(sum.HashSum)
}
for _, p := range parts {
if p.PartNumber == nil {
continue
}
md5sum := hr.MD5SumPart(*p.PartNumber)
op = append(op, &ObjectPart{
PartNumber: p.PartNumber,
Size: p.Size,
ChecksumCRC32: checksumObject(p.ChecksumCRC32),
ChecksumCRC32C: checksumObject(p.ChecksumCRC32C),
ChecksumSHA1: checksumObject(p.ChecksumSHA1),
ChecksumSHA256: checksumObject(p.ChecksumSHA256),
ChecksumMD5: NewObjectChecksum(md5sum),
})
}
return op
}
type ObjectPart struct {
PartNumber *int32
Size *int64
ChecksumCRC32 *ObjectChecksum `json:",omitempty"`
ChecksumCRC32C *ObjectChecksum `json:",omitempty"`
ChecksumSHA1 *ObjectChecksum `json:",omitempty"`
ChecksumSHA256 *ObjectChecksum `json:",omitempty"`
ChecksumMD5 *ObjectChecksum `json:",omitempty"`
}
// UploadPartError represents an error recorded in an
// S3UploadState.uploadPartsError entry.
type UploadPartError struct {
PartNumber int32
Error string
}
// ObjectErrors captures any errors recorded in an S3UploadState
type ObjectErrors struct {
PutObjectError string `json:",omitempty"`
UploadPartErrors []*UploadPartError `json:",omitempty"`
CompleteMultipartUploadError string `json:",omitempty"`
AbortMultipartUploadError string `json:",omitempty"`
GetObjectAttributesError string `json:",omitempty"`
}
func NewObjectErrors(st *S3UploadState) *ObjectErrors {
var uploadPartErrors []*UploadPartError
for i, err := range st.uploadPartErrors {
if err == nil {
continue
}
uploadPartErrors = append(uploadPartErrors, &UploadPartError{
PartNumber: int32(i + 1),
Error: errorString(err),
})
}
return &ObjectErrors{
PutObjectError: errorString(st.objError),
UploadPartErrors: uploadPartErrors,
CompleteMultipartUploadError: errorString(st.completedError),
AbortMultipartUploadError: errorString(st.abortedError),
GetObjectAttributesError: errorString(st.objectAttributesError),
}
}
// errorString returns the string form of an error
func errorString(err error) string {
if err != nil {
return err.Error()
}
return ""
}