flydrive
is a framework-agnostic package which provides a powerful wrapper to manage file Storage in Node.js.
There are currently 3 drivers available:
'local'
: Stores files on the local file system.'s3'
: Amazon S3 and other compatible services
@slynova/flydrive-s3
package to be able to use this driver.'gcs'
: Google Cloud Storage
@slynova/flydrive-gcs
package to be able to use this driver.This package is available in the npm registry.
It can easily be installed with npm
or yarn
.
$ npm i @slynova/flydrive
# or
$ yarn add @slynova/flydrive
When you require the package in your file, it will give you access to the StorageManager
class.
This class is a facade for the package and should be instantiated with a configuration object.
const { StorageManager } = require('@slynova/flydrive');
const storage = new StorageManager(...);
Once you instantiated the manager, you can use the StorageManager#disk()
method to retrieve a disk an use it.
storage.disk(); // Returns the default disk (specified in the config)
storage.disk('awsCloud'); // Returns the driver for the disk "s3"
storage.disk('awsCloud', customConfig); // Overwrite the default configuration of the disk
After installing any external driver, like @slynova/flydrive-gcs
, you need to register it inside our manager to be able to use it.
The following is done by using the method storage.registerDriver(name: string, Driver)
.
const { GoogleCloudStorage } = require('@slynova/flydrive-gcs');
const { StorageManager } = require('@slynova/flydrive');
const storage = new StorageManager(...);
storage.registerDriver('gcs', GoogleCloudStorage);
Each driver extends the abstract class Storage
. This class will throw an exception for each methods by default. The driver needs to overwrite the methods it supports.
The following method doesn't exist on the LocalFileSystemStorage
driver, therefore, it will throw an exception.
// throws "E_METHOD_NOT_SUPPORTED: Method getSignedUrl is not supported for the driver LocalFileSystemStorage"
storage.disk('local').getSignedUrl();
Since we are using TypeScript, you can make use of casting to get the real interface:
import { LocalFileSystemStorage } from '@slynova/flydrive';
storage.disk<LocalFileSystemStorage>('local');
Asynchronous methods will always return a Promise which resolves with a Response
object. The response object may contain relevant data in its properties (for
example, the ExistsResponse
object for the exists
method contains a boolean
exists
property).
All responses additionally have a raw
property which is driver-specific and
contains the result from the original call made by the driver.
In case of runtime errors, flydrive
will try to throw driver-agnostic exceptions.
Exceptions also have a raw
property which contains the original error.
append(location: string, content: Buffer | Stream | string, options: object): Promise<Response>
copy(src: string, dest: string, options: object): Promise<Response>
delete(location: string): Promise<DeleteResponse>
driver()
exists(location: string): Promise<ExistsResponse>
get(location: string, encoding: string = 'utf-8'): Promise<ContentResponse<string>>
getBuffer(location: string): Promise<ContentResponse<Buffer>>
getSignedUrl(location: string, options: SignedUrlOptions = { expiry: 900 }): Promise<SignedUrlResponse>
getStat(location: string): Promise<StatResponse>
getStream(location: string, options: object | string): Stream
getUrl(location: string): string
move(src: string, dest: string): Promise<Response>
put(location: string, content: Buffer | Stream | string, options: object): Promise<Response>
prepend(location: string, content: Buffer | string, options: object): Promise<Response>
flatList(prefix?: string): AsyncIterable<FileListResponse>
Any pull requests or discussions are welcome. Note that every pull request providing new feature or correcting a bug should be created with appropriate unit tests.