Recipes
Persist uploads to storage
import { LocalStorage } from '@quik/storage-express';
import { Decorators, QRoute } from '@quik/http';
@Decorators.Route.Route('/uploads')
export class UploadRoute extends QRoute {
@LocalStorage.Decorators.SingleFilePersist('file')
@Decorators.Endpoint.POST('/persist')
async uploadAndPersist(event) {
return { file: event.context.files?.file };
}
}
SingleFilePersist/ArrayFilePersist accept an optional destination path to override the configured
storage.local.destination. LocalStorageFile.moveToDestination is async; LocalStorageFile.extension includes
the leading dot (e.g. .txt).
Serve a signed download URL
This package does not register GET/POST routes for signed local storage URLs — mount your own route and
verify the token with RequireLocalStorageToken:
import { Decorators, QQueryParameters, QRoute, Responses } from '@quik/http';
import { Fields } from '@quik/entity';
import { LOCAL_STORAGE_TOKEN_PATH_KEY, RequireLocalStorageToken } from '@quik/storage-express';
import { QStorageEngineType, StorageStore } from '@quik/storage';
import type { QLocalStorageEngine } from '@quik/storage';
@Decorators.Parameters.Query()
class TokenQuery extends QQueryParameters {
@Fields.String()
public token: string;
}
@Decorators.Route.API('/storage/local')
export class LocalStorageRoute extends QRoute {
@Decorators.Endpoint.GET('/download', { query: TokenQuery })
@RequireLocalStorageToken('download')
async download(event) {
const path = event.getObject<string>(LOCAL_STORAGE_TOKEN_PATH_KEY);
const engine = StorageStore.get(QStorageEngineType.LOCAL) as QLocalStorageEngine;
const file = await engine.get(path);
return Responses.OKFile(file.path, file.mimeType);
}
}
Generate the URL to hand to a client with @quik/storage's QLocalStorageEngine.getSignedDownloadURL/getSignedUploadURL
or PresignedLocalURLEntity. There is no Bearer authentication involved — the signed token itself is the authorization.