S3 Object Store
Allow storing json objects in S3 easily
Goals:
- Map objects to a sensible s3 hierarchy
- Use json for fast-ish and flexible serialization and model updates + human readability
- Control the folder template name with options
Please see the Sandbox project, Program.cs
which shows a simple example of using sessions.
Usage
Define an object that implements the IStorageObject
interface:
public sealed class Session : IStorageObject
{
[JsonPropertyName("k")]
public string? Key { get; set; } = string.Empty;
[JsonPropertyName("o")]
public string? Owner { get; set; }
[JsonPropertyName("i")]
public string IPAddress { get; set; } = string.Empty;
[JsonPropertyName("a")]
public string UserAgent { get; set; } = string.Empty;
[JsonPropertyName("e")]
public DateTimeOffset Expires { get; set; }
[JsonPropertyName("p")]
public string Permissions { get; set; } = string.Empty;
public override string ToString()
{
return $"{Key} {Owner} {IPAddress} {UserAgent} {Expires} {Permissions}";
}
}
Using JsonPropertyName
to shorten property names is highly recommended as it will save you on storage space.
The IStorageObject
interface will return null for both Key
and Owner
by default.
In most cases, you will usually implement both the Key
and Owner
properties yourself and return Guid
or some other identifier.
Exceptions to this case:
- You can return null for the
Owner
if your service options (see down below) do not have a format specifier for the owner in the folder template - {0}
.
- You can return null for the
Key
if your service options specify that the folder format has the file name in it. By default, the Key
is used as the file name, with a .json
extension.
Create your s3 repository
var config = new S3Config(accessKey, secretKey, url, disableSigning);
var repository = new S3StorageRepository(config, new FakeEnvironment(), new NullLogger<S3StorageRepository>());
Create your object service
var serviceOptions = new StorageObjectServiceOptions<Session>
{
Bucket = "bucketname",
FolderFormat = "users/{0}/sessions",
FolderFormatIncludesFileName = false
};
var service = new S3StorageObjectService<Session>(serviceOptions, repository);
Perform operations
The storage object service interface is as follows:
public interface IStorageObjectService<T> where T : class, IStorageObject
{
Task<T?> GetObjectAsync(string key, string owner);
Task SetObjectAsync(T obj);
Task<IReadOnlyCollection<T>> GetObjectsAsync(string owner);
Task<IReadOnlyCollection<string>> GetKeys(string owner);
Task DeleteObjectAsync(string key, string owner);
}
Please email support@digitalruby.com if you have questions or feedback.
-- Jeff