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.
- 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
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 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');
// 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 !
});
});
});
A Store instance represents the Object Storage API service. It is defined by a URL of where the service is hosted.
Create a new store from a URL.
url
<String> Object storage API url. Defaults tohttp://127.0.0.1
Attempts to list activated capabilities on this Store, will fail if not configured on OpenStack.
Returns a <Promise>
resolves
to <Object>
a json object containing the list of capabilities on success,
or rejects
with a <Error>.
URL
<String>: The URL in use by this Store can be read usinggetUrl
and updated withsetUrl
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.
Create a new <Account> instance, main constructor.
store
<Store> os² Store instance where the account resides.username
<String> Username used authenticate. Defaults tonull
.password
<String> Password used authenticate. Defaults tonull
.storage_url
<String> URL pointing to this account storage API.storage_token
<String> Authentication token to use when querying the API.
Alternative constructor for construction from a URL and a username/password.
storeUrl
<String> An OpenStack Object Storage URL.username
<String> Username used for authentication.password
<String> Password used for authentication.
Returns a new <Account> instance.
Alternative constructor for construction from a URL and an account name and authentication token
storeUrl
<String> An OpenStack Object Storage URL.name
<String> As used by OpenStack in this account storage URL.token
<String> A valid authentication token to connecting this account.
Returns a new <Account> instance.
Performs this account connection with the Store. Uses the /auth/v1.0 authentication mechanism of swift object storage
Returns a <Promise>, which resolves
to true on success or rejects
a native <Error> on failure.
Disconnects this account from the store.
Returns <Promise> that always resolves to true.
List all the containers in this account.
Returns <Promise> that resolves to a json array of containers on success, rejects a native <Error> otherwise.
Retrieve stored metadata for this account, MUST be connected.
Returns <Promise>. Resolves to an object containing all the metadata on success, reject an <Error> otherwise
Update metadata for this account. Omitted metadata items are unchanged,
metadata items with null
or undefined
values are removed.
metadata
<Object> each property is considered to be a metadata item.
Returns <Promise>.
Resolves
to true on success, rejects
a native js <Error> on failure
connected
<Boolean> Account's connection status.True
if a valid authentication has been performed. Access it withisConnected
store
<Store> An account is backed by a store instance.getStore
andsetStore
are available.username
<String> Username used for authentication.getUsername
andsetUsername
password
<String> Password used for authentication.getPassword
andsetPassword
token
<String> Authentication token from the store tempAuth. This property is read only:getToken
storageUrl
<String> Once authenticated, url where this account objects are. Read only :getStorageUrl
name
<String> Name of the account in the store, exctracted fromstrorageUrl
or passed at contruction. Read only:getName
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.
Creates a new Container instance.
account
<Account> A valid instance of Account the new container will belong to.name
<String> Is the namespace representing the container on the object store.
Creates or updates the container instance in the object storage.
Returns a <Promise> which resolves
to the object storage response on success, on error it rejects
a native js Error.
Delete the container instance in the object storage.
Returns a <Promise> which resolves
to the object storage response on success, on error it rejects
a native js Error.
Update metadata associated with the container. Omitted metadata items are unchanged,
metadata items with null
or undefined
values are removed.
metadata
<Object> each property is considered to be a metadata item.
Returns <Promise>.
and resolves
to true on success, rejects
a native js <Error> on failure
Retrieve the stored metadata in this container.
Returns <Promise>. Resolves to an object containing all the metadata on success, reject an <Error> otherwise
Get details and objects list from the container.
Returns a <Promise> which resolves
to the json content of the container
or rejects
an <Error> instance.
account
<Account> A valid Account instance where the container belongs.getAccount
andsetAccount
are available.name
<String> The container identifier.getName
andsetName
are available.
Stores data content, such as documents, images, and so on. You can also store custom metadata with an object.
Create a new Segment/Object in the container.
container
<Container> An instance of a container for the Segmentname
<String> As the name or identifier for the Segment
Assign the segment's content from a file on disk, replaces its content if already exists.
filepath
<String> Absolute or relative path to the file on disk
Returns a <Promise>. It resolves
to true on success, on error it rejects
a <Error>
Assign the segment's content from stream, replaces its content if already exists
readStream
Segment content in the form of a Node.js <stream.Readable> instance
Returns a <Promise>. It resolves
to true on success, on error it rejects
a <Error>
Delete this object form the Object Storage.
Returns a <Promise>. It resolves
to true on success, on error it rejects
a <Error>
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>. It resolves
to true on success, on error it rejects
a <Error>
Get a stream readable on the segment content
Returns a <Promise>. It resolves
to a <stream.Readable>
on success. A javascript <Error> is rejected
on error.
Update metadata associated with the Segment. Omitted metadata items are unchanged,
metadata items with null
or undefined
values are removed.
metadata
<Object> each property is considered to be a metadata item.
Returns <Promise>.
and resolves
to true on success, rejects
a native js <Error> on failure
Retrieve the stored metadata with this segment.
Returns <Promise>. Resolves to an object containing all the metadata on success, reject an <Error> otherwise
name
<String> Identifier or name for the segment.getName
andsetName
methods are provided.container
<Container> An instance of a container the segment is stored into.getContainer
andsetContainer
are available.
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.
Create a new DynamicLargeObject or DLO.
container
<Container> An instance of a container for the DLOname
<String> As the name or identifier of the DLO
Creates or updates this DLO manifest
Returns <Promise>.
and resolves
to true on success, rejects
a js <Error> otherwise.
Creates a SLO from a single stream.
stream
<stream.Readable> A readable stream providing the contentchunkSize
Optional maximum size of the generated segments. Default and max to 1Go
Returns <Promise>.
Resolves
to a map of segments:status on success and rejects
a js <Error> otherwise.
Create a dlo from a file on disk. The file gets split in segments if needed.
path
<String> Path of the source file on the disk.chunkSize
Optional maximum size of the generated segments. Default to 5Go.
Returns <Promise>.
Resolves
to a map of segments:status on success and rejects
a js <Error> otherwise.
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
streams
<Array> An array of streams to get the data from.
Returns <Promise>.
Resolves
to a map of segments:status on success and rejects
a js <Error> otherwise.
Get the DLO content or its manifest content.
manifest
<Boolean> Set to true to get the manifest, false for the content. Defaults tofalse
.
Returns a <Promise>. It resolves
to a <stream.Readable>
on success. A javascript <Error> is rejected
on error.
delete
copy
getMetadata
setMetadata
prefix
<String> A DLO has a prefix to identify which segments are part of the DLO. Accessors aregetPrefix
andsetPrefix
.
name
container
A StaticLargeObject contains multiple segments, and is defined by a manifest.
Creates a new StaticLargeObject or SLO.
container
<Container> An instance of a container for the SLOname
<String> As the name or identifier of the SLO.
Creates or updates this SLO manifest
manifestContent
A json <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>.
and resolves
to true on success, rejects
a native js <Error> on failure
Creates a SLO from a single stream.
stream
<stream.Readable> A readable stream providing the contentchunkSize
Optional maximum size of the generated segments. Default and max to 1Go
Returns <Promise>.
Resolves
to a map of segments:status on success and rejects
a js <Error> otherwise.
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
streams
<Array> An array of streams to get the data from.
Returns <Promise>.
Resolves
to a map of segments:status on success and rejects
a js <Error> otherwise.
Get the SLO content or its manifest content.
manifest
<Boolean> Set to true to get the manifest, false for the content. Defaults tofalse
.
Returns a <Promise>. It resolves
to a <stream.Readable>
on success. A javascript <Error> is rejected
on error.
Delete the static large object and the segments it refers to.
Returns a <Promise>.
and resolves
to true on success, rejects
a native js <Error> on failure
delete
copy
getMetadata
setMetadata
createFromDisk
createFromStream
name
container
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());
});