Skip to content

Commit ab835c4

Browse files
authored
Merge pull request #130 from TraGicCode/chore/add-support-for-aws-s3-bucket-for-large-messages
Add S3 Bucket support for amazon sqs transport config
2 parents 8f9e1de + c078662 commit ab835c4

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

src/BuslyCLI.Console/Config/AmazonsqsTransportConfig.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ public class AmazonsqsTransportConfig : ITransportConfig
1010
public string AccessKey { get; set; }
1111
public string SecretKey { get; set; }
1212

13+
public AwsS3BucketSettings S3BucketSettings { get; set; }
14+
15+
}
16+
17+
public class AwsS3BucketSettings
18+
{
19+
public string BucketName { get; set; }
20+
public string KeyPrefix { get; set; }
1321
}

src/BuslyCLI.Console/Config/Validators/AmazonsqsTransportConfigValidator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,21 @@ public AmazonsqsTransportConfigValidator()
1515
|| (!string.IsNullOrEmpty(x.AccessKey) && !string.IsNullOrEmpty(x.SecretKey)) // both set
1616
)
1717
.WithMessage("AWS AccessKey and SecretKey are mutually dependent: if one is set, the other must also be set.");
18+
19+
RuleFor(x => x.S3BucketSettings)
20+
.SetValidator(new AwsS3BucketSettingsValidator());
21+
}
22+
}
23+
24+
public class AwsS3BucketSettingsValidator : AbstractValidator<AwsS3BucketSettings>
25+
{
26+
public AwsS3BucketSettingsValidator()
27+
{
28+
RuleFor(x => x.BucketName)
29+
.NotEmpty();
30+
31+
RuleFor(x => x.KeyPrefix)
32+
.NotEmpty();
33+
1834
}
1935
}

src/BuslyCLI.Console/Factories/RawEndpointFactory.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Amazon;
22
using Amazon.Runtime;
3+
using Amazon.S3;
34
using Amazon.SimpleNotificationService;
45
using Amazon.SQS;
56
using BuslyCLI.Config;
@@ -77,6 +78,7 @@ private TransportDefinition CreateAmazonSQSTransport(AmazonsqsTransportConfig am
7778
var credentials = new BasicAWSCredentials(amazonsqsTransportConfig.AccessKey, amazonsqsTransportConfig.SecretKey);
7879
var amazonSqsConfig = new AmazonSQSConfig();
7980
var amazonSnsConfig = new AmazonSimpleNotificationServiceConfig();
81+
var amazonS3Config = new AmazonS3Config();
8082
if (!string.IsNullOrWhiteSpace(amazonsqsTransportConfig.RegionName))
8183
{
8284

@@ -92,10 +94,21 @@ private TransportDefinition CreateAmazonSQSTransport(AmazonsqsTransportConfig am
9294
amazonSqsConfig.ServiceURL = amazonsqsTransportConfig.ServiceUrl;
9395
}
9496

95-
var sqsClient = new AmazonSQSClient(credentials, amazonSqsConfig);
97+
if (amazonsqsTransportConfig.S3BucketSettings is not null)
98+
{
99+
amazonS3Config.ServiceURL = amazonsqsTransportConfig.ServiceUrl;
100+
amazonS3Config.RegionEndpoint = RegionEndpoint.GetBySystemName(amazonsqsTransportConfig.RegionName);
101+
}
96102

103+
var sqsClient = new AmazonSQSClient(credentials, amazonSqsConfig);
97104
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, amazonSnsConfig);
98105

106+
var sqsTransport = new SqsTransport(sqsClient, snsClient);
107+
if (amazonsqsTransportConfig.S3BucketSettings is not null)
108+
{
109+
sqsTransport.S3 = new S3Settings(amazonsqsTransportConfig.S3BucketSettings.BucketName, amazonsqsTransportConfig.S3BucketSettings.KeyPrefix, new AmazonS3Client(amazonS3Config));
110+
}
111+
99112
return new SqsTransport(sqsClient, snsClient);
100113
}
101114

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using BuslyCLI.Config;
2+
using BuslyCLI.Config.Validators;
3+
using FluentValidation.TestHelper;
4+
5+
namespace BuslyCLI.Console.Tests.Config.Validators;
6+
7+
8+
[TestFixture]
9+
public class AwsS3BucketSettingsValidatorTests
10+
{
11+
private readonly AwsS3BucketSettingsValidator _validator;
12+
13+
public AwsS3BucketSettingsValidatorTests()
14+
{
15+
_validator = new AwsS3BucketSettingsValidator();
16+
}
17+
18+
[Test]
19+
public async Task ShouldErrorWhenBucketNameIsNotPassed()
20+
{
21+
// Arrange
22+
var awsS3BucketSettings = new AwsS3BucketSettings()
23+
{
24+
BucketName = null
25+
};
26+
27+
// Act
28+
var result = await _validator.TestValidateAsync(awsS3BucketSettings);
29+
30+
// Assert
31+
result.ShouldHaveValidationErrorFor(c => c.BucketName)
32+
.WithErrorMessage("'Bucket Name' must not be empty.");
33+
}
34+
35+
[Test]
36+
public async Task ShouldErrorWhenKeyPrefixIsNotPassed()
37+
{
38+
// Arrange
39+
var awsS3BucketSettings = new AwsS3BucketSettings()
40+
{
41+
KeyPrefix = null
42+
};
43+
44+
// Act
45+
var result = await _validator.TestValidateAsync(awsS3BucketSettings);
46+
47+
// Assert
48+
result.ShouldHaveValidationErrorFor(c => c.KeyPrefix)
49+
.WithErrorMessage("'Key Prefix' must not be empty.");
50+
}
51+
}

website/docs/transports/amazon-sqs.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ The Amazon SQS transport implementation currently works only with **AWS access k
3939

4040
---
4141

42+
## `s3-bucket-settings` Fields
43+
44+
| Field | Required | Type | Default | Description |
45+
| ------------- | -------- | ------ | ------- | ------------------------------------------------------------------------ |
46+
| `bucket-name` | yes | string | — | Name of the S3 bucket for storing large messages. |
47+
| `key-prefix` | yes | string | — | This is the path within the specified S3 bucket to store large messages. |
48+
49+
---
50+
4251
## Field Details
4352

4453
### `region-name` (required)
@@ -86,3 +95,17 @@ Examples:
8695
```yaml
8796
service-url: http://127.0.0.1:32813/
8897
```
98+
99+
---
100+
101+
### `s3-bucket-settings` (optional)
102+
103+
Used to configure S3 bucket storage for large messages.
104+
105+
Examples:
106+
107+
```yaml
108+
s3-bucket-settings:
109+
bucket-name: nsb-sqs-messages
110+
key-prefix: my/sample/path
111+
```

0 commit comments

Comments
 (0)