Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MailerQ.Test/MessageStorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void Constructor_should_throw_exception_with_not_implemented_supported_en
[InlineData(SimpleValidMongoUri)]
[InlineData("mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary")]
[InlineData("mongodb://mongos1.example.com,mongos2.example.com/database?readPreference=secondary")]
[InlineData("s3://accesskey:secretkey@region/bucketname")]
[Theory]
public void Constructor_should_create_instance_with_valid_supported_engine_uri(string uri)
{
Expand Down
1 change: 1 addition & 0 deletions MailerQ/MailerQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.3.22" />
<PackageReference Include="EasyNetQ" Version="6.5.2" />
<PackageReference Include="MongoDB.Driver" Version="2.16.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
Expand Down
5 changes: 5 additions & 0 deletions MailerQ/MessageStore/IMessageStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface IMessageStorage
/// <returns></returns>
Task<string> InsertAsync(string message, int secondsToExpire = DefaultSecondsToExpire, CancellationToken cancellationToken = default);

/// <summary>
/// The default storage name to use if not indicated explicitly
/// </summary>
const string DefaultStorageName = "mailerq";

/// <summary>
/// Message time to live into the storage engine before expire. Default 25 hours
/// </summary>
Expand Down
11 changes: 5 additions & 6 deletions MailerQ/MessageStore/MessageStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public MessageStorage(IOptions<MailerQConfiguration> options)

private IMessageStorage CreateConcretStorage(StorageEngines storageEngine, string uri)
{
switch (storageEngine)
return storageEngine switch
{
case StorageEngines.MongoDB:
return new MongoDBMessageStorage(uri);
default:
throw new NotImplementedException($"{storageEngine} message storage engine is not implement yet.");
}
StorageEngines.MongoDB => new MongoDBMessageStorage(uri),
StorageEngines.S3 => new S3MessageStorage(uri),
_ => throw new NotImplementedException($"{storageEngine} message storage engine is not implement yet."),
};
}

/// <inheritdoc/>
Expand Down
3 changes: 1 addition & 2 deletions MailerQ/MessageStore/MongoDBMessageStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ namespace MailerQ.MessageStore
internal class MongoDBMessageStorage : IMessageStorage
{
private const int MessageMaxSuppportedSize = 15728640; // 15 MB
private const string DataBase = "mailerq";
private readonly string Collection = "mime";
private readonly IMongoCollection<BsonDocument> messages;

public MongoDBMessageStorage(string url)
{
var mongoUrl = MongoUrl.Create(url);
var mongoClient = new MongoClient(mongoUrl);
var database = mongoClient.GetDatabase(mongoUrl.DatabaseName ?? DataBase);
var database = mongoClient.GetDatabase(mongoUrl.DatabaseName ?? IMessageStorage.DefaultStorageName);
messages = database.GetCollection<BsonDocument>(Collection);
}

Expand Down
35 changes: 35 additions & 0 deletions MailerQ/MessageStore/S3MessageStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MailerQ.MessageStore
{
internal class S3MessageStorage : IMessageStorage
{
private readonly IAmazonS3 _client;

public S3MessageStorage(string url)
{
var config = new AmazonS3Config { ServiceURL = url };
_client = new AmazonS3Client(config);
}

public async Task<string> InsertAsync(string message, int secondsToExpire, CancellationToken cancellationToken)
{
var key = Guid.NewGuid().ToString();
var request = new PutObjectRequest
{
BucketName = IMessageStorage.DefaultStorageName,
Key = key,
ContentType = "text/plain",
ContentBody = message,
};

await _client.PutObjectAsync(request, cancellationToken);

return key;
}
}
}