0.0.13 • Published 6 years ago

os2 v0.0.13

Weekly downloads
9
License
MIT
Repository
github
Last release
6 years ago

os²

OpenStack Object Storage wrapper for Node.js

circle.ci badge

os² is a wrapper for the OpenStack Object Storage API v1. Use it to communicate with an Object Storage via it's REST api without managing the HTTP requests.

Features

  • Abstractions to write and read data from an Object Storage using Node.js streams.
  • Authentication to the OpenStack Object Storage using the TempAuth method. (No production, contributions are welcome)
  • Asynchronous communication using ES6 Promise
  • Support for Large Objects

Getting started

Installation

Installing globally from the npm package manager:

npm install os2 --global

Or add os² as a dependency to a node.js project using the package.json file:

"dependencies": {
  "os2": "0.0.1"
}

and then update the project's dependencies

npm install

Import

Import os² components as any Node.js package. For example, all os² components at once :

const os2 = require('os2');

Or, to import os² components separately:

// pick only what you need, lol
const { Store, Account, Container, Segment, DynamicLargeObject, StaticLargeObject } = require('os2');

Example

// First, create a Store to connect to
let store = new Store('http://example.openstackobjectstorage:8080/');
    
// Then, make an account, give it the store and the credentials
let account = new Account(store, 'username', 'password');
    
// Authenticate with tempAuth
account.connect().then(function() {
    
        //We can now create containers using the connected account
        let container = new Container(account, 'example_container');
        
        //Perform the container creation on the object storage API
        container.create().then(function() {
    
            //Create an object inside the container
            let obj = new Segment(container, 'example_segment');
                
            //Put the content of a file into: example_container/example_segment
            obj.createFromDisk('./path/to/my/file.txt').then(function(ok) {
               // Yeah the file is created ! 
            });
        });
    });

Quick reference

  1. Store
  2. Account
  3. Container
  4. Segment
  5. Dynamic Large Object
  6. Static Large Object
  7. Error handling

Store

A Store instance represents the Object Storage API service. It is defined by a URL of where the service is hosted.

Methods

new Store([url])

Create a new store from a URL.

info()

Attempts to list activated capabilities on this Store, will fail if not configured on OpenStack.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ resolves to \[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\ a json object containing the list of capabilities on success, or rejects with a \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\.

Properties

Account

Your service provider creates your account and you own all resources in that account. The account defines a namespace for containers. In the OpenStack environment, account is synonymous with a project or tenant.

Methods

new Account(store, [username, [password, [storage_url, token]]])

Create a new \<Account> instance, main constructor.

Account.fromUsernameAndPassword(storeUrl, username, password)

Alternative constructor for construction from a URL and a username/password.

Returns a new \<Account> instance.

Account.fromNameAndToken(storeUrl, name, token)

Alternative constructor for construction from a URL and an account name and authentication token

Returns a new \<Account> instance.

connect()

Performs this account connection with the Store. Uses the /auth/v1.0 authentication mechanism of swift object storage

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\, which resolves to true on success or rejects a native \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure.

disconnect()

Disconnects this account from the store.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ that always resolves to true.

listContainers()

List all the containers in this account.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ that resolves to a json array of containers on success, rejects a native \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

getMetadata()

Retrieve stored metadata for this account, MUST be connected.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to an object containing all the metadata on success, reject an \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise

setMetadata(metadata)

Update metadata for this account. Omitted metadata items are unchanged, metadata items with null or undefined values are removed.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to true on success, rejects a native js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure

Properties

Container

Defines a namespace for objects. An object with the same name in two different containers represents two different objects. There can be any number of containers within an account.

Methods

new Container(account, name)

Creates a new Container instance.

create()

Creates or updates the container instance in the object storage.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ which resolves to the object storage response on success, on error it rejects a native js Error.

delete()

Delete the container instance in the object storage.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ which resolves to the object storage response on success, on error it rejects a native js Error.

setMetadata(metadata)

Update metadata associated with the container. Omitted metadata items are unchanged, metadata items with null or undefined values are removed.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. and resolves to true on success, rejects a native js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure

getMetadata()

Retrieve the stored metadata in this container.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to an object containing all the metadata on success, reject an \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise

listObjects()

Get details and objects list from the container. Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\ which resolves to the json content of the container or rejects an \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ instance.

Properties

Segment

Stores data content, such as documents, images, and so on. You can also store custom metadata with an object.

Methods

new Segment(container, name)

Create a new Segment/Object in the container.

createFromDisk(filepath)

Assign the segment's content from a file on disk, replaces its content if already exists.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to true on success, on error it rejects a \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\

createFromStream(readStream)

Assign the segment's content from stream, replaces its content if already exists

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to true on success, on error it rejects a \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\

delete()

Delete this object form the Object Storage.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to true on success, on error it rejects a \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\

copy

Copies this object to the destination object. If the destination object is already created in the Object storage, it is replaced If the source segment is a Large Object, the manifest is copied, referencing the same content.

  • object \<Segment> As a destination object

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to true on success, on error it rejects a \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\

getContentStream()

Get a stream readable on the segment content

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to a \[stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable)\ on success. A javascript \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ is rejected on error.

setMetadata(metadata)

Update metadata associated with the Segment. Omitted metadata items are unchanged, metadata items with null or undefined values are removed.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. and resolves to true on success, rejects a native js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure

getMetadata()

Retrieve the stored metadata with this segment.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to an object containing all the metadata on success, reject an \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise

Properties

DLO

A DynamicLargeObject contains multiple Segments. It is defined by a container: where the segment objects are stored; and a prefix: a string that all segment objects have in common.

Methods

new DynamicLargeObject(container, name, prefix) {

Create a new DynamicLargeObject or DLO.

createManifest()

Creates or updates this DLO manifest

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. and resolves to true on success, rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

createFromStream

Creates a SLO from a single stream.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to a map of segments:status on success and rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

createFromDisk(filePath, chunkSize)

Create a dlo from a file on disk. The file gets split in segments if needed.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to a map of segments:status on success and rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

createFromStreams(streams)

Create a DLO from multiple data streams, where each stream is stored as a segment. The created DLO contains the concatenated content of the streams, ordered as received

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to a map of segments:status on success and rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

getContentStream(manifest)

Get the DLO content or its manifest content.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to a \[stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable)\ on success. A javascript \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ is rejected on error.

Inherited methods from Segment

  • delete
  • copy
  • getMetadata
  • setMetadata

Properties

Inherited properties from Segment

  • name
  • container

SLO

A StaticLargeObject contains multiple segments, and is defined by a manifest.

Methods

new StaticLargeObject(container, name)

Creates a new StaticLargeObject or SLO.

createManifest(manifestContent)

Creates or updates this SLO manifest

  • manifestContent A json \[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\, where each element is an object representing a segment. These objects may contain the following attributes:
    • path (required). The container and object name in the format: {container-name}/{object-name}
    • etag (optional). If provided, this value must match the ETag of the segment object. This was included in the response headers when the segment was created. Generally, this will be the MD5 sum of the segment.
    • size_bytes (optional). The size of the segment object. If provided, this value must match the Content-Length of that object.
    • range (optional). The subset of the referenced object that should be used for segment data. This behaves similar to the Range header. If omitted, the entire object will be used.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. and resolves to true on success, rejects a native js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure

createFromStream

Creates a SLO from a single stream.

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to a map of segments:status on success and rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

createFromStreams(streams)

Create a SLO from multiple data streams, where each stream is stored as a segment. The created SLO contains the concatenated content of the streams, ordered as received

Returns \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. Resolves to a map of segments:status on success and rejects a js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ otherwise.

getContentStream(manifest)

Get the SLO content or its manifest content.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. It resolves to a \[stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable)\ on success. A javascript \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ is rejected on error.

deleteWithContent()

Delete the static large object and the segments it refers to.

Returns a \[Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)\. and resolves to true on success, rejects a native js \[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)\ on failure

Inherited methods from Segment

  • delete
  • copy
  • getMetadata
  • setMetadata

Inherited methods from DynamicLargeObject

  • createFromDisk
  • createFromStream

Inherited properties from Segment

  • name
  • container

Error

When an operation on the Object Storage API timeouts or the HTTP status code indicates an error, the Promise will reject a native javascript Error containing an error message.

let account = new Account(example_store, username, password);
account.connect().then(function() {
  // Code here
}, function (error) {
console.error(error.toString());
});
0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago