@targos/flydrive
Flexible and Fluent way to manage storage in Node.js.
Last updated a year ago by targos .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @targos/flydrive 
SYNC missed versions from official npm registry.

flydrive

Download Version License

flydrive is a framework-agnostic package which provides a powerful wrapper to manage Storage in Node.js.

There are currently 4 drivers available:

  • Local
  • Amazon S3 (You need to install aws-sdk package to be able to use this driver)
  • Digital Ocean Spaces (You need to install aws-sdk package to be able to use this driver)
  • Google Cloud Storage (You need to install @google-cloud/storage package to be able to use this driver)

Getting Started

This package is available in the npm registry. It can easily be installed with npm or yarn.

$ npm i @targos/flydrive
# or
$ yarn add @targos/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('@targos/flydrive')
const storage = new StorageManager(config)

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

Driver's API

Each driver extends the abstract class Storage. This class will throw an exception for each methods by default. The driver needs to overwrite those methods when he supports them.

The following method doesn't exists on the LocalFileSystem driver, therefore, it will throw an exception.

// throws "E_METHOD_NOT_SUPPORTED: Method getSignedUrl is not supported for the driver LocalFileSystem"
storage.disk('local').getSignedUrl()

Since we are using TypeScript, you can make use of casting to get the real interface:

import { LocalFileSystem } from '@targos/flydrive'

storage.disk<LocalFileSystem>('local')

Methods

<summary markdown="span"> append(location: string, content: Buffer | Stream | string, options: object): Promise<boolean></summary>

This method will append the content to the file at the location.

// Supported drivers: "local"

await storage.disk('local').append('./foo.txt', 'bar')
// foo.txt now has the content `{initialContent}bar`
<summary markdown="span"> bucket(name: string): void</summary>

This method let us swap the used bucker at the runtime.

// Supported drivers: "s3", "gcs"

storage.disk('cloud').bucket('anotherOne')
// The following chained action will use the "anotherOne" bucket instead of the default one
<summary markdown="span"> delete(location: string): Promise<boolean></summary>

This method will delete the file at the given location

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').delete('./foo.txt')
// foo.txt has been deleted
<summary markdown="span"> driver(): any</summary>

This method returns the driver used if you need to do anything specific not supported by default.

storage.disk('local').driver() // Returns "fs-extra"
storage.disk('awsCloud').driver() // Returns "aws-sdk"
storage.disk('googleCloud').driver() // Returns "@google-cloud/storage"
// ....
<summary markdown="span"> exists(location: string): Promise<boolean></summary>

This method will determine if a file or folder exists for the given location.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').exists('./foo.txt')
<summary markdown="span"> get(location: string, encoding?: object | string): Promise<Buffer | string></summary>

This methods will return the file's content for the given location.

// Supported drivers: "local", "s3", "gcs"

const content = await storage.disk('local').exists('./foo.txt')
<summary markdown="span"> getSignedUrl(location: string, expiry: number = 900): Promise<string></summary>

This methods will return the signed url for an existing file.

// Supported drivers: "s3", "gcs"

const uri = await storage.disk('awsCloud').getSignedUrl('./foo.txt')
<summary markdown="span"> getSize(location: string): Promise<number></summary>

This methods will return the file size in bytes.

// Supported drivers: "local", "gcs"

const bytes = await storage.disk('local').getSize('./foo.txt.)
<summary markdown="span"> getStream(location: string, options: object | string): Stream</summary>

This methods will return a stream for the given file.

// Supported drivers: "local", "s3", "gcs"

const stream = storage.disk('local').getStream('./foo.txt')
<summary markdown="span"> getUrl(location: string): string</summary>

This methods will return an url for a given file.

// Supported drivers: "s3", "gcs"

const uri = storage.disk('awsCloud').getUrl('./foo.txt')
<summary markdown="span"> move(src: string, dest: string): Promise<boolean></summary>

This methods will move file to a new location.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').move('./foo.txt', './newFolder/foo.txt')
<summary markdown="span"> put(location: string, content: Buffer | Stream | string, options: object): Promise<boolean></summary>

This methods will create a new file.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').put('./bar.txt', 'Foobar')
<summary markdown="span"> prepend(location: string, content: Buffer | string, options: object): Promise<boolean></summary>

This methods will preprend content to a file.

// Supported drivers: "local"

await storage.disk('local').prepend('./foo.txt', 'bar')
// foo.txt now has the content `bar{initialContent}`

Contribution Guidelines

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.

Current Tags

  • 0.4.2                                ...           latest (a year ago)

3 Versions

  • 0.4.2                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (13)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |