This project was inspired on the great PHP package Flysystem, and tries to be API compatible when possible. It's a work in progress. Pull requests are welcome.
Like Flysystem, is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.
It's made with Typescript. All methods are async.
- Have a generic API for handling common tasks across multiple file storage engines.
- Have consistent output which you can rely on.
- Emulate directories in systems that support none, like AwsS3.
Using YARN:
yarn add node-filesystem
Using NPM:
npm add node-filesystem --save
See Flysysytem docs
await filesystem.write('path/to/file.txt', 'contents');
await filesystem.update('path/to/file.txt', 'new contents');
await filesystem.put('path/to/file.txt', 'contents');
const contents = await filesystem.read('path/to/file.txt');
const exists = await filesystem.has('path/to/file.txt');
NOTE: This only has consistent behaviour for files, not directories. Directories are less important, they’re created implicitly and often ignored because not every adapter (filesystem type) supports directories.
await filesystem.delete('path/to/file.txt');
await filesystem.rename('filename.txt', 'newname.txt');
await filesystem.copy('filename.txt', 'duplicate.txt');
const mimetype = await filesystem.getMimetype('path/to/file.txt');
const timestamp = await filesystem.getTimestamp('path/to/file.txt');
const size = await filesystem.getSize('path/to/file.txt');
await filesystem.createDir('path/to/nested/directory');
await filesystem.write('path/to/file.txt', 'contents');
await filesystem.deleteDir('path/to/directory');
The above method will delete directories recursively
NOTE: All paths used are relative to the adapter root directory.
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
await filesystem.write('db.backup', backup, {
visibility: 'private',
});
You can also change and check visibility of existing files
if ((await filesystem.getVisibility('secret.txt')) === 'private') {
await filesystem.setVisibility('secret.txt', 'public');
}
const contents = await filesystem.listContents();
The result of a contents listing is a collection of arrays containing all the metadata the file manager knows at that time. By default you’ll receive path info and file type. Additional info could be supplied by default depending on the adapter used.
Example:
for (const object of contents) {
console.log(
object.basename,
' is located at ',
object.path,
' and is a ',
object.type,
);
}
By default it lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results
const contents = filesystem.listContents('some/dir', true);
import { LocalAdapter } from 'node-filesystem';
new LocalAdapter('my-root-folder', 'my-subfolder');
You need install the oficial AWS SDK:
yarn add aws-sdk
import * as AWS from 'aws-sdk';
import { S3Adapter } from 'node-filesystem';
const s3Client = new AWS.S3({
accessKeyId: 'my-aws-access-key',
secretAccessKey: 'my-aws-secret-key',
region: 'my-aws-region',
});
new S3Adapter(s3Client, 'my-bucket', 'my-subfolder');
import * as Storage from '@google-cloud/storage';
import { GoogleStorage } from 'node-filesystem';
const googleStorageClient = new Storage({
projectId: '',
});
process.env.GOOGLE_APPLICATION_CREDENTIALS =
__dirname + '/../gcp-credentials.json';
new GoogleStorage(googleStorageClient, 'my-bucket', 'my-subfolder');
You can use the AWS S3 Adapter