Skip to content

Commit 6749987

Browse files
Merge pull request #30 from FrendsPlatform/FSPW-260
Add AWS Enhanced Logging and dotenv Configuration Support
2 parents ca4111b + 30fca3b commit 6749987

File tree

13 files changed

+316
-100
lines changed

13 files changed

+316
-100
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
# User-specific files (MonoDevelop/Xamarin Studio)
1111
*.userprefs
1212

13+
# Dotenv
14+
.env
15+
1316
# Build results
1417
[Dd]ebug/
1518
[Dd]ebugPublic/

Frends.AmazonS3.UploadObject/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## [2.0.0] - 2025-05-21
4+
### Changed
5+
- Updated AWS SDK to version 4.0.0.5
6+
- Changed the logging logic to use custom logger
7+
### Added
8+
- [Breaking]Added GatherDebugLog option to allow disabling the DebugLog, `false` by default
9+
- By default, the gathering debug log is now disabled.
10+
- To replicate the previous task behaviour set GatherDebugLog parameter to `true`
11+
312
## [1.3.0] - 2025-03-13
413
### Fixed
514
- Fixed issue where ThrowErrorIfNoMatch was not correctly handled. Now, no error is thrown if set to false, and an error is thrown if set to true when no files match."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HiQ_AWSS3Test_AccessKey=...
2+
HiQ_AWSS3Test_SecretAccessKey=...
3+
HiQ_AWSS3Test_BucketName=...

Frends.AmazonS3.UploadObject/Frends.AmazonS3.UploadObject.Test/AWSCredsUnitTests.cs

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,48 @@
88
using System.IO;
99
using System.Linq;
1010
using System.Threading.Tasks;
11+
using dotenv.net;
1112

1213
namespace Frends.AmazonS3.UploadObject.Tests;
1314

1415
[TestClass]
1516
public class AWSCredsUnitTests
1617
{
1718
public TestContext? TestContext { get; set; }
18-
private readonly string? _accessKey = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_AccessKey");
19-
private readonly string? _secretAccessKey = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_SecretAccessKey");
20-
private readonly string? _bucketName = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_BucketName");
19+
private readonly string? _accessKey;
20+
private readonly string? _secretAccessKey;
21+
private readonly string? _bucketName;
2122
private readonly string _dir = Path.Combine(Environment.CurrentDirectory);
2223
Connection? _connection;
2324
Input? _input;
2425

26+
public AWSCredsUnitTests()
27+
{
28+
DotEnv.Load();
29+
_accessKey = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_AccessKey");
30+
_secretAccessKey = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_SecretAccessKey");
31+
_bucketName = Environment.GetEnvironmentVariable("HiQ_AWSS3Test_BucketName");
32+
}
33+
2534
[TestInitialize]
2635
public void Initialize()
2736
{
28-
Directory.CreateDirectory($@"{_dir}\AWS");
29-
Directory.CreateDirectory($@"{_dir}\AWS\Subfolder");
30-
Directory.CreateDirectory($@"{_dir}\AWS\EmptyFolder");
31-
32-
File.AppendAllText($@"{_dir}\AWS\test1.txt", "test1");
33-
File.AppendAllText($@"{_dir}\AWS\Subfolder\subfile.txt", "From subfolder.");
34-
File.AppendAllText($@"{_dir}\AWS\deletethis_awscreds.txt", "Resource file deleted. (AWS Creds)");
35-
File.AppendAllText($@"{_dir}\AWS\overwrite_presign.txt", "Not overwriten. (Presign)");
36-
File.AppendAllText($@"{_dir}\AWS\overwrite_awscreds.txt", "Not overwriten. (AWS creds)");
37+
Directory.CreateDirectory(Path.Combine(_dir, "AWS"));
38+
Directory.CreateDirectory(Path.Combine(_dir, "AWS", "Subfolder"));
39+
Directory.CreateDirectory(Path.Combine(_dir, "AWS", "EmptyFolder"));
40+
41+
File.AppendAllText(Path.Combine(_dir, "AWS", "test1.txt"), "test1");
42+
File.AppendAllText(Path.Combine(_dir, "AWS", "Subfolder", "subfile.txt"), "From subfolder.");
43+
File.AppendAllText(Path.Combine(_dir, "AWS", "deletethis_awscreds.txt"), "Resource file deleted. (AWS Creds)");
44+
File.AppendAllText(Path.Combine(_dir, "AWS", "overwrite_presign.txt"), "Not overwriten. (Presign)");
45+
File.AppendAllText(Path.Combine(_dir, "AWS", "overwrite_awscreds.txt"), "Not overwriten. (AWS creds)");
3746
}
3847

3948
[TestCleanup]
4049
public async Task CleanUp()
4150
{
42-
if (Directory.Exists($@"{_dir}\AWS"))
43-
Directory.Delete($@"{_dir}\AWS", true);
51+
if (Directory.Exists(Path.Combine(_dir, "AWS")))
52+
Directory.Delete(Path.Combine(_dir, "AWS"), true);
4453

4554
using var client = new AmazonS3Client(_accessKey, _secretAccessKey, RegionEndpoint.EUCentral1);
4655

@@ -52,6 +61,7 @@ public async Task CleanUp()
5261
var response = await client.ListObjectsAsync(listObjectRequest);
5362
var objects = response.S3Objects;
5463

64+
if (objects == null) return;
5565
foreach (var obj in objects)
5666
{
5767
var deleteObjectRequest = new DeleteObjectRequest
@@ -68,7 +78,7 @@ public async Task AWSCreds_Upload()
6878
{
6979
_input = new Input
7080
{
71-
FilePath = $@"{_dir}\AWS",
81+
FilePath = Path.Combine(_dir, "AWS"),
7282
ACL = default,
7383
FileMask = null,
7484
UseACL = false,
@@ -89,6 +99,7 @@ public async Task AWSCreds_Upload()
8999
DeleteSource = false,
90100
ThrowErrorIfNoMatch = false,
91101
UseMultipartUpload = false,
102+
GatherDebugLog = true
92103
};
93104

94105
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -98,12 +109,48 @@ public async Task AWSCreds_Upload()
98109
Assert.IsTrue(result.UploadedObjects.Any(x => x.Contains("deletethis_awscreds.txt")));
99110
}
100111

112+
[TestMethod]
113+
public async Task AWSCreds_Upload_GatherDebugLog_False()
114+
{
115+
_input = new Input
116+
{
117+
FilePath = Path.Combine(_dir, "AWS"),
118+
ACL = default,
119+
FileMask = null,
120+
UseACL = false,
121+
S3Directory = "Upload2023/",
122+
};
123+
_connection = new Connection
124+
{
125+
AuthenticationMethod = AuthenticationMethod.AWSCredentials,
126+
PreSignedURL = null,
127+
AwsAccessKeyId = _accessKey,
128+
AwsSecretAccessKey = _secretAccessKey,
129+
BucketName = _bucketName,
130+
Region = Region.EuCentral1,
131+
UploadFromCurrentDirectoryOnly = false,
132+
Overwrite = false,
133+
PreserveFolderStructure = false,
134+
ReturnListOfObjectKeys = false,
135+
DeleteSource = false,
136+
ThrowErrorIfNoMatch = false,
137+
UseMultipartUpload = false,
138+
GatherDebugLog = false,
139+
};
140+
141+
var result = await AmazonS3.UploadObject(_connection, _input, default);
142+
Assert.AreEqual(5, result.UploadedObjects.Count);
143+
Assert.IsTrue(result.Success);
144+
Assert.IsNull(result.DebugLog);
145+
Assert.IsTrue(result.UploadedObjects.Any(x => x.Contains("deletethis_awscreds.txt")));
146+
}
147+
101148
[TestMethod]
102149
public async Task AWSCreds_Missing_ThrowExceptionOnErrorResponse_False()
103150
{
104151
_input = new Input
105152
{
106-
FilePath = $@"{_dir}\AWS",
153+
FilePath = Path.Combine(_dir, "AWS"),
107154
ACL = default,
108155
FileMask = null,
109156
UseACL = false,
@@ -125,6 +172,7 @@ public async Task AWSCreds_Missing_ThrowExceptionOnErrorResponse_False()
125172
ThrowErrorIfNoMatch = false,
126173
ThrowExceptionOnErrorResponse = false,
127174
UseMultipartUpload = false,
175+
GatherDebugLog = true,
128176
};
129177

130178
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -138,7 +186,7 @@ public async Task AWSCreds_Missing_ThrowExceptionOnErrorResponse_True()
138186
{
139187
_input = new Input
140188
{
141-
FilePath = $@"{_dir}\AWS",
189+
FilePath = Path.Combine(_dir, "AWS"),
142190
ACL = default,
143191
FileMask = null,
144192
UseACL = false,
@@ -160,6 +208,7 @@ public async Task AWSCreds_Missing_ThrowExceptionOnErrorResponse_True()
160208
ThrowErrorIfNoMatch = false,
161209
ThrowExceptionOnErrorResponse = true,
162210
UseMultipartUpload = false,
211+
GatherDebugLog = true,
163212
};
164213

165214
var ex = await Assert.ThrowsExceptionAsync<UploadException>(async () => await AmazonS3.UploadObject(_connection, _input, default));
@@ -171,7 +220,7 @@ public async Task AWSCreds_UploadFromCurrentDirectoryOnly()
171220
{
172221
_input = new Input
173222
{
174-
FilePath = $@"{_dir}\AWS",
223+
FilePath = Path.Combine(_dir, "AWS"),
175224
ACL = default,
176225
FileMask = null,
177226
UseACL = false,
@@ -192,6 +241,7 @@ public async Task AWSCreds_UploadFromCurrentDirectoryOnly()
192241
DeleteSource = false,
193242
ThrowErrorIfNoMatch = false,
194243
UseMultipartUpload = false,
244+
GatherDebugLog = true,
195245
};
196246

197247
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -206,7 +256,7 @@ public async Task AWSCreds_Overwrite()
206256
{
207257
_input = new Input
208258
{
209-
FilePath = $@"{_dir}\AWS",
259+
FilePath = Path.Combine(_dir, "AWS"),
210260
ACL = default,
211261
FileMask = null,
212262
UseACL = false,
@@ -227,6 +277,7 @@ public async Task AWSCreds_Overwrite()
227277
DeleteSource = false,
228278
ThrowErrorIfNoMatch = false,
229279
UseMultipartUpload = false,
280+
GatherDebugLog = true,
230281
};
231282

232283
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -241,7 +292,7 @@ public async Task AWSCreds_PreserveFolderStructure()
241292
{
242293
_input = new Input
243294
{
244-
FilePath = $@"{_dir}\AWS",
295+
FilePath = Path.Combine(_dir, "AWS"),
245296
ACL = default,
246297
FileMask = null,
247298
UseACL = false,
@@ -262,6 +313,7 @@ public async Task AWSCreds_PreserveFolderStructure()
262313
DeleteSource = false,
263314
ThrowErrorIfNoMatch = false,
264315
UseMultipartUpload = false,
316+
GatherDebugLog = true,
265317
};
266318

267319
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -276,7 +328,7 @@ public async Task AWSCreds_ReturnListOfObjectKeys()
276328
{
277329
_input = new Input
278330
{
279-
FilePath = $@"{_dir}\AWS",
331+
FilePath = Path.Combine(_dir, "AWS"),
280332
ACL = default,
281333
FileMask = null,
282334
UseACL = false,
@@ -297,6 +349,7 @@ public async Task AWSCreds_ReturnListOfObjectKeys()
297349
DeleteSource = false,
298350
ThrowErrorIfNoMatch = false,
299351
UseMultipartUpload = false,
352+
GatherDebugLog = true,
300353
};
301354

302355
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -313,7 +366,7 @@ public async Task AWSCreds_DeleteSourceFile_Mask()
313366
var fileName = "deletethis_awscreds.txt";
314367
_input = new Input
315368
{
316-
FilePath = $@"{_dir}\AWS",
369+
FilePath = Path.Combine(_dir, "AWS"),
317370
ACL = default,
318371
FileMask = fileName,
319372
UseACL = false,
@@ -334,22 +387,23 @@ public async Task AWSCreds_DeleteSourceFile_Mask()
334387
UploadFromCurrentDirectoryOnly = false,
335388
ThrowErrorIfNoMatch = false,
336389
UseMultipartUpload = false,
390+
GatherDebugLog = true,
337391
};
338392

339393
var result = await AmazonS3.UploadObject(_connection, _input, default);
340394
Assert.AreEqual(1, result.UploadedObjects.Count);
341395
Assert.IsTrue(result.Success);
342396
Assert.IsNotNull(result.DebugLog);
343397
Assert.IsTrue(result.UploadedObjects.Any(x => x.Contains("deletethis_awscreds.txt")));
344-
Assert.IsFalse(File.Exists($@"{_dir}\AWS\{fileName}"));
398+
Assert.IsFalse(File.Exists(Path.Combine(_dir, "AWS", fileName)));
345399
}
346400

347401
[TestMethod]
348402
public async Task AWSCreds_ThrowErrorIfNoMatch()
349403
{
350404
_input = new Input
351405
{
352-
FilePath = $@"{_dir}\AWS",
406+
FilePath = Path.Combine(_dir, "AWS"),
353407
ACL = default,
354408
FileMask = "notafile*",
355409
UseACL = false,
@@ -380,7 +434,9 @@ public async Task AWSCreds_ThrowErrorIfNoMatch()
380434
[TestMethod]
381435
public async Task AWSCreds_ACLs()
382436
{
383-
var acls = new List<ACLs>() { ACLs.Private, ACLs.PublicRead, ACLs.PublicReadWrite, ACLs.AuthenticatedRead, ACLs.BucketOwnerRead, ACLs.BucketOwnerFullControl, ACLs.LogDeliveryWrite };
437+
// Public ACLs like PublicRead, PublicReadWrite and AuthenticatedRead are
438+
// excluded from automated unit tests due to security
439+
var acls = new List<ACLs> { ACLs.Private, ACLs.BucketOwnerRead, ACLs.BucketOwnerFullControl, ACLs.LogDeliveryWrite };
384440

385441
_connection = new Connection
386442
{
@@ -397,21 +453,22 @@ public async Task AWSCreds_ACLs()
397453
DeleteSource = false,
398454
ThrowErrorIfNoMatch = false,
399455
UseMultipartUpload = false,
456+
GatherDebugLog = true
400457
};
401458

402459
foreach (var acl in acls)
403460
{
404461
_input = new Input
405462
{
406-
FilePath = $@"{_dir}\AWS",
463+
FilePath = Path.Combine(_dir, "AWS"),
407464
ACL = acl,
408465
FileMask = null,
409466
UseACL = true,
410467
S3Directory = "Upload2023/",
411468
};
412469

413470
var result = await AmazonS3.UploadObject(_connection, _input, default);
414-
Assert.AreEqual(5, result.UploadedObjects.Count);
471+
Assert.AreEqual(5, result.UploadedObjects.Count, acl + Environment.NewLine + result.DebugLog);
415472
Assert.IsTrue(result.Success);
416473
Assert.IsNotNull(result.DebugLog);
417474
Assert.IsTrue(result.UploadedObjects.Any(x => x.Contains("deletethis_awscreds.txt")));
@@ -426,7 +483,7 @@ public async Task AWSCreds_Upload_ShouldNotThrow_IfEmptyFolder_AndThrowErrorIfNo
426483
{
427484
_input = new Input
428485
{
429-
FilePath = $@"{_dir}\AWS\EmptyFolder",
486+
FilePath = Path.Combine(_dir, "AWS", "EmptyFolder"),
430487
ACL = default,
431488
FileMask = null,
432489
UseACL = false,
@@ -447,6 +504,7 @@ public async Task AWSCreds_Upload_ShouldNotThrow_IfEmptyFolder_AndThrowErrorIfNo
447504
DeleteSource = false,
448505
ThrowErrorIfNoMatch = false,
449506
UseMultipartUpload = false,
507+
GatherDebugLog = true,
450508
};
451509

452510
var result = await AmazonS3.UploadObject(_connection, _input, default);
@@ -460,7 +518,7 @@ public async Task AWSCreds_Upload_ShouldThrow_IfEmptyFolder_AndThrowErrorIfNoMat
460518
{
461519
_input = new Input
462520
{
463-
FilePath = $@"{_dir}\AWS\EmptyFolder",
521+
FilePath = Path.Combine(_dir, "AWS", "EmptyFolder"),
464522
ACL = default,
465523
FileMask = null,
466524
UseACL = false,
@@ -486,4 +544,5 @@ public async Task AWSCreds_Upload_ShouldThrow_IfEmptyFolder_AndThrowErrorIfNoMat
486544
var ex = await Assert.ThrowsExceptionAsync<Exception>(async () => await AmazonS3.UploadObject(_connection, _input, default));
487545
Assert.IsTrue(ex.Message.Contains($"No files match the filemask '*' within supplied path."));
488546
}
547+
489548
}

0 commit comments

Comments
 (0)