@@ -14,6 +14,7 @@ import (
14
14
"github.com/aws/aws-sdk-go/service/s3/s3manager"
15
15
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
16
16
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
17
+ "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
17
18
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/metrics"
18
19
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/tracing"
19
20
)
@@ -68,10 +69,34 @@ func (s3cl *s3client) buildGetObjectInputFromInput(input *GetInput) *s3.GetObjec
68
69
return s3Input
69
70
}
70
71
71
- func (s3cl * s3client ) GetObjectSignedURL (_ context.Context , input * GetInput , expiration time.Duration ) (string , error ) {
72
+ func (s3cl * s3client ) GetObjectSignedURL (ctx context.Context , input * GetInput , expiration time.Duration ) (string , error ) {
72
73
// Build input
73
74
s3Input := s3cl .buildGetObjectInputFromInput (input )
74
75
76
+ // Get trace
77
+ parentTrace := tracing .GetTraceFromContext (ctx )
78
+ // Create child trace
79
+ childTrace := parentTrace .GetChildTrace ("s3-bucket.get-object-signed-url-request" )
80
+ childTrace .SetTag ("s3-bucket.bucket-name" , s3cl .target .Bucket .Name )
81
+ childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
82
+ childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
83
+ childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
84
+ childTrace .SetTag ("s3-bucket.bucket-key" , * s3Input .Key )
85
+ childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
86
+
87
+ defer childTrace .Finish ()
88
+
89
+ // Get logger
90
+ logger := log .GetLoggerFromContext (ctx )
91
+ // Build logger
92
+ logger = logger .WithFields (map [string ]interface {}{
93
+ "bucket" : s3cl .target .Bucket .Name ,
94
+ "key" : * s3Input .Key ,
95
+ "region" : s3cl .target .Bucket .Region ,
96
+ })
97
+ // Log
98
+ logger .Debugf ("Trying to get object presigned url" )
99
+
75
100
// Build object request
76
101
req , _ := s3cl .svcClient .GetObjectRequest (s3Input )
77
102
// Build url
@@ -93,11 +118,17 @@ func (s3cl *s3client) GetObjectSignedURL(_ context.Context, input *GetInput, exp
93
118
return "" , errors .WithStack (err )
94
119
}
95
120
121
+ // Log
122
+ logger .Debug ("Get object presigned url with success" )
123
+
96
124
return urlStr , nil
97
125
}
98
126
99
127
// ListFilesAndDirectories List files and directories.
100
128
func (s3cl * s3client ) ListFilesAndDirectories (ctx context.Context , key string ) ([]* ListElementOutput , * ResultInfo , error ) {
129
+ // Get logger
130
+ logger := log .GetLoggerFromContext (ctx )
131
+
101
132
// List files on path
102
133
folders := make ([]* ListElementOutput , 0 )
103
134
files := make ([]* ListElementOutput , 0 )
@@ -131,8 +162,19 @@ func (s3cl *s3client) ListFilesAndDirectories(ctx context.Context, key string) (
131
162
childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
132
163
childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
133
164
childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
165
+ childTrace .SetTag ("s3-bucket.bucket-key" , key )
134
166
childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
135
167
168
+ // Build logger
169
+ logger = logger .WithFields (map [string ]interface {}{
170
+ "bucket" : s3cl .target .Bucket .Name ,
171
+ "key" : key ,
172
+ "region" : s3cl .target .Bucket .Region ,
173
+ "maxKeys" : maxKeys ,
174
+ })
175
+ // Log
176
+ logger .Debugf ("Trying to list objects" )
177
+
136
178
// Request S3
137
179
err := s3cl .svcClient .ListObjectsV2PagesWithContext (
138
180
ctx ,
@@ -200,6 +242,9 @@ func (s3cl *s3client) ListFilesAndDirectories(ctx context.Context, key string) (
200
242
if err != nil {
201
243
return nil , nil , errors .WithStack (err )
202
244
}
245
+
246
+ // Log
247
+ logger .Debugf ("List objects done with success" )
203
248
}
204
249
205
250
// Concat folders and files
@@ -219,6 +264,9 @@ func (s3cl *s3client) ListFilesAndDirectories(ctx context.Context, key string) (
219
264
220
265
// GetObject Get object from S3 bucket.
221
266
func (s3cl * s3client ) GetObject (ctx context.Context , input * GetInput ) (* GetOutput , * ResultInfo , error ) {
267
+ // Build input
268
+ s3Input := s3cl .buildGetObjectInputFromInput (input )
269
+
222
270
// Get trace
223
271
parentTrace := tracing .GetTraceFromContext (ctx )
224
272
// Create child trace
@@ -227,19 +275,28 @@ func (s3cl *s3client) GetObject(ctx context.Context, input *GetInput) (*GetOutpu
227
275
childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
228
276
childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
229
277
childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
278
+ childTrace .SetTag ("s3-bucket.bucket-key" , * s3Input .Key )
230
279
childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
231
280
232
281
defer childTrace .Finish ()
233
282
234
- // Build input
235
- s3Input := s3cl .buildGetObjectInputFromInput (input )
236
-
237
283
// Init & get request headers
238
284
var requestHeaders map [string ]string
239
285
if s3cl .target .Bucket .RequestConfig != nil {
240
286
requestHeaders = s3cl .target .Bucket .RequestConfig .GetHeaders
241
287
}
242
288
289
+ // Get logger
290
+ logger := log .GetLoggerFromContext (ctx )
291
+ // Build logger
292
+ logger = logger .WithFields (map [string ]interface {}{
293
+ "bucket" : s3cl .target .Bucket .Name ,
294
+ "key" : * s3Input .Key ,
295
+ "region" : s3cl .target .Bucket .Region ,
296
+ })
297
+ // Log
298
+ logger .Debugf ("Trying to get object" )
299
+
243
300
obj , err := s3cl .svcClient .GetObjectWithContext (
244
301
ctx ,
245
302
s3Input ,
@@ -324,10 +381,21 @@ func (s3cl *s3client) GetObject(ctx context.Context, input *GetInput) (*GetOutpu
324
381
Key : input .Key ,
325
382
}
326
383
384
+ // Log
385
+ logger .Debugf ("Get object done with success" )
386
+
327
387
return output , info , nil
328
388
}
329
389
330
390
func (s3cl * s3client ) PutObject (ctx context.Context , input * PutInput ) (* ResultInfo , error ) {
391
+ // Build input
392
+ inp := & s3manager.UploadInput {
393
+ Body : input .Body ,
394
+ Bucket : aws .String (s3cl .target .Bucket .Name ),
395
+ Key : aws .String (input .Key ),
396
+ Expires : input .Expires ,
397
+ }
398
+
331
399
// Get trace
332
400
parentTrace := tracing .GetTraceFromContext (ctx )
333
401
// Create child trace
@@ -336,16 +404,21 @@ func (s3cl *s3client) PutObject(ctx context.Context, input *PutInput) (*ResultIn
336
404
childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
337
405
childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
338
406
childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
407
+ childTrace .SetTag ("s3-bucket.bucket-key" , * inp .Key )
339
408
childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
340
409
341
410
defer childTrace .Finish ()
342
411
343
- inp := & s3manager.UploadInput {
344
- Body : input .Body ,
345
- Bucket : aws .String (s3cl .target .Bucket .Name ),
346
- Key : aws .String (input .Key ),
347
- Expires : input .Expires ,
348
- }
412
+ // Get logger
413
+ logger := log .GetLoggerFromContext (ctx )
414
+ // Build logger
415
+ logger = logger .WithFields (map [string ]interface {}{
416
+ "bucket" : s3cl .target .Bucket .Name ,
417
+ "key" : * inp .Key ,
418
+ "region" : s3cl .target .Bucket .Region ,
419
+ })
420
+ // Log
421
+ logger .Debugf ("Trying to put object" )
349
422
350
423
// Manage ACL
351
424
if s3cl .target .Actions != nil &&
@@ -416,6 +489,9 @@ func (s3cl *s3client) PutObject(ctx context.Context, input *PutInput) (*ResultIn
416
489
Key : input .Key ,
417
490
}
418
491
492
+ // Log
493
+ logger .Debugf ("Put object done with success" )
494
+
419
495
// Return
420
496
return info , nil
421
497
}
@@ -429,10 +505,22 @@ func (s3cl *s3client) HeadObject(ctx context.Context, key string) (*HeadOutput,
429
505
childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
430
506
childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
431
507
childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
508
+ childTrace .SetTag ("s3-bucket.bucket-key" , key )
432
509
childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
433
510
434
511
defer childTrace .Finish ()
435
512
513
+ // Get logger
514
+ logger := log .GetLoggerFromContext (ctx )
515
+ // Build logger
516
+ logger = logger .WithFields (map [string ]interface {}{
517
+ "bucket" : s3cl .target .Bucket .Name ,
518
+ "key" : key ,
519
+ "region" : s3cl .target .Bucket .Region ,
520
+ })
521
+ // Log
522
+ logger .Debugf ("Trying to head object" )
523
+
436
524
// Init & get request headers
437
525
var requestHeaders map [string ]string
438
526
if s3cl .target .Bucket .RequestConfig != nil {
@@ -469,6 +557,10 @@ func (s3cl *s3client) HeadObject(ctx context.Context, key string) (*HeadOutput,
469
557
Type : FileType ,
470
558
Key : key ,
471
559
}
560
+
561
+ // Log
562
+ logger .Debugf ("Head object done with success" )
563
+
472
564
// Return output
473
565
return output , nil
474
566
}
@@ -482,10 +574,22 @@ func (s3cl *s3client) DeleteObject(ctx context.Context, key string) (*ResultInfo
482
574
childTrace .SetTag ("s3-bucket.bucket-region" , s3cl .target .Bucket .Region )
483
575
childTrace .SetTag ("s3-bucket.bucket-prefix" , s3cl .target .Bucket .Prefix )
484
576
childTrace .SetTag ("s3-bucket.bucket-s3-endpoint" , s3cl .target .Bucket .S3Endpoint )
577
+ childTrace .SetTag ("s3-bucket.bucket-key" , key )
485
578
childTrace .SetTag ("s3-proxy.target-name" , s3cl .target .Name )
486
579
487
580
defer childTrace .Finish ()
488
581
582
+ // Get logger
583
+ logger := log .GetLoggerFromContext (ctx )
584
+ // Build logger
585
+ logger = logger .WithFields (map [string ]interface {}{
586
+ "bucket" : s3cl .target .Bucket .Name ,
587
+ "key" : key ,
588
+ "region" : s3cl .target .Bucket .Region ,
589
+ })
590
+ // Log
591
+ logger .Debugf ("Trying to delete object" )
592
+
489
593
// Init & get request headers
490
594
var requestHeaders map [string ]string
491
595
if s3cl .target .Bucket .RequestConfig != nil {
@@ -517,6 +621,9 @@ func (s3cl *s3client) DeleteObject(ctx context.Context, key string) (*ResultInfo
517
621
Key : key ,
518
622
}
519
623
624
+ // Log
625
+ logger .Debugf ("Delete object done with success" )
626
+
520
627
// Return
521
628
return info , nil
522
629
}
0 commit comments