npm.io
4.1.0 • Published 2d ago

tiny-storage-client

Licence
Apache-2.0
Version
4.1.0
Deps
2
Size
191 kB
Vulns
0
Weekly
0
Stars
17

Tiny client for distributed S3/Swift storages

GitHub release (latest by date) Documentation

High availability, Performance, and Simplicity are the main focus of this tiny Node client to request AWS S3 API or the OpenStack Swift Object Storage API. It was initially made to request OVHCloud, but it can be used for any Server/Cloud provider.

Highlights

  • Simple to use - Only 10 core methods: uploadFile, downloadFile, deleteFile, listFiles, listBuckets, headBucket, setFileMetadata, getFileMetadata, deleteFiles and request for custom requests. A few helpers come on top: setTimeout, setConfig/getConfig, setLogFunction and setRockReqDefaults/getRockReqDefaults.
  • Performance - Vanilla JS + Only 2 dependencies rock-req as HTTP client and aws4 for signing S3 requests.
  • High availability - Provide one or a list of storages credentials: the SDK will switch storage if something goes wrong (Server/DNS not responding, timeout, repeated 5xx errors, authentication error). A throttled request (429, S3 503 SlowDown, Swift 498) is handled as before (a 429 / 498 is returned, an S3 503 goes to the next storage), optionally after backoff retries on the same storage (maxThrottleRetry). As soon as the main storage is available, the SDK returns to the main storage (S3: checked at the next request after a switch, then every 60 s doubling up to 10 min while it stays down; Swift: at most every 60 s, up to 10 min). See S3 and Swift.
  • Reconnect automatically - If a request fails due to an authentication token expiration, the SDK fetches a new authentication token and retries the initial request with it (Swift Storage only).
  • Battle-tested - Used in production against hundreds of TBs of file uploads, downloads and deletes, and covered by an extensive test suite.
  • Convert XML/JSON responses to JS - XML/JSON responses are automatically converted into Objects/Arrays (only for: listFiles, listBuckets and Errors).
  • Mixing S3 and Swift credentials is not supported - When initialising the Tiny SDK client, provide only a list of S3 or a list of Swift credentials, switching from one storage system to another is not supported.

Install

$ npm install tiny-storage-client

Requires Node.js 20 or higher.

Getting Started

Install and setup in less than 2 minutes:

Supported Methods

Swift API S3 API Method Description
example example uploadFile Upload a file from a Buffer or file absolute path.
example example downloadFile Download a file as Buffer or Stream
example example deleteFile Delete a file
example example deleteFiles Bulk delete files (1000 max/per request)
example example listFiles List files (1000 max/per requests) use query parameters for pagination
example example getFileMetadata Fetch custom metadata
example example setFileMetadata Set custom file metadata
example example headBucket Determine if a bucket exists and you have permission to access it
example example listBuckets Returns a list of all buckets owned by the authenticated sender of the request.
example example request Create custom requests
example example Bucket Alias Simplify requests by using bucket alias

S3 Example

The following example is an initialisation of the SDK client with a list of S3 credentials and a request to download a file. If something goes wrong when downloading the file, the SDK will switch storage and retry to download with the second credentials. As soon as the first storage is available, the SDK returns to the main storage

const storageClient = require('tiny-storage-client');

const s3storage = storageClient([{
    accessKeyId    : 'accessKeyId',
    secretAccessKey: 'secretAccessKey',
    url            : 's3.gra.io.cloud.ovh.net',
    region         : 'gra'
  },
  {
    accessKeyId    : 'accessKeyId',
    secretAccessKey: 'secretAccessKey',
    url            : 's3.eu-west-3.amazonaws.com',
    region         : 'eu-west-3'
  }])

s3storage.downloadFile('bucketName', 'filename.pdf', (err, resp) => {
  if (err) {
    return console.log("Error on download: ", err);
  }
  /**
   * Request response:
   * - resp.body => downloaded file as Buffer
   * - resp.headers
   * - resp.statusCode
   */
})
S3 storage class and OVHcloud endpoints

Each S3 storage accepts an optional storageClass, sent as x-amz-storage-class on uploadFile and setFileMetadata (unless you set the header). After a failover, the class of the storage actually used applies. The values are provider specific: on OVHcloud (endpoint s3.<region>.io.cloud.ovh.net), EXPRESS_ONEZONE is High Performance in 1-AZ regions and STANDARD is Standard; MinIO and Cloudflare R2 reject an unknown value with a 400. See Storage class.

On OVHcloud, prefer the endpoint s3.<region>.io.cloud.ovh.net to the legacy perf endpoint.

Swift Example

The same client is used for OpenStack Swift, only the credentials differ. The first request authenticates automatically (the SDK renews the token when it expires).

const storageClient = require('tiny-storage-client');

const swiftStorage = storageClient([{
    username: 'username',
    password: 'password',
    authUrl : 'https://auth.cloud.ovh.net/v3',
    region  : 'GRA'
  },
  {
    username: 'username',
    password: 'password',
    authUrl : 'https://auth.cloud.ovh.net/v3',
    region  : 'SBG'
  }])

swiftStorage.downloadFile('containerName', 'filename.pdf', (err, resp) => {
  if (err) {
    return console.log("Error on download: ", err);
  }
  /**
   * Request response:
   * - resp.body => downloaded file as Buffer
   * - resp.headers
   * - resp.statusCode
   */
})

Run tests

Install

$ npm install

To run all the tests:

$ npm run test

Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ️ if this project helped you!

License

Apache-2.0

Supporters

This package is maintained by Carbone:

Carbone.io logo

Keywords