(core) Add AzureExternalStorage

Summary:
Adds a new implementation of the interface ExternalStorage that works for Azure Blob Storage as an alternative to S3, for a specific self-hosting case.

Tweaks HostedStorageManager and ICreate to allow configuring different core implementations of ExternalStorage.

Followup tasks:

- Make this code available to self hosters, possibly by making it open source.
- Add an env var or other config option to specify the preferred type of storage. Currently using the var `AZURE_STORAGE_CONNECTION_STRING` to know how to connect to Azure when requested, but that choice still only lives in test code.

Test Plan: Generalized HostedStorageManager and ExternalStorage tests to test the new AzureExternalStorage alongside S3ExternalStorage. The HostedStorageManager tests also now test the 'cached' in-memory test storage in a way that's closer to the real storage methods.

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D3413
This commit is contained in:
Alex Hall
2022-05-09 21:05:19 +02:00
parent 52eb5325c2
commit 4408315f2e
4 changed files with 74 additions and 7 deletions

View File

@@ -35,3 +35,35 @@ export interface DocSnapshot extends ObjSnapshotWithMetadata {
export interface DocSnapshots {
snapshots: DocSnapshot[];
}
/**
* Metadata format for external storage like S3 and Azure.
* The only difference is that external metadata values must be strings.
*
* For S3, there are restrictions on total length of metadata (2 KB).
* See: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#UserMetadata
*/
type ExternalMetadata = Record<string, string>;
/**
* Convert metadata from internal Grist format to external storage format (string values).
*/
export function toExternalMetadata(metadata: ObjMetadata): ExternalMetadata {
const result: ExternalMetadata = {};
for (const [key, val] of Object.entries(metadata)) {
if (val !== undefined) { result[key] = String(val); }
}
return result;
}
/**
* Select metadata controlled by Grist, and convert to expected formats.
*/
export function toGristMetadata(metadata: ExternalMetadata): ObjMetadata {
const result: ObjMetadata = {};
for (const key of ['t', 'tz', 'h', 'label'] as const) {
if (metadata[key]) { result[key] = metadata[key]; }
}
if (metadata.n) { result.n = parseInt(metadata.n, 10); }
return result;
}

View File

@@ -0,0 +1,25 @@
/**
* Just some basic utilities for async generators that should really be part of the language or lodash or something.
*/
export async function* asyncFilter<T>(it: AsyncIterableIterator<T>, pred: (x: T) => boolean): AsyncIterableIterator<T> {
for await (const x of it) {
if (pred(x)) {
yield x;
}
}
}
export async function* asyncMap<T, R>(it: AsyncIterableIterator<T>, mapper: (x: T) => R): AsyncIterableIterator<R> {
for await (const x of it) {
yield mapper(x);
}
}
export async function toArray<T>(it: AsyncIterableIterator<T>): Promise<T[]> {
const result = [];
for await (const x of it) {
result.push(x);
}
return result;
}