Skip to content

dimer47/node-ovh-objectstorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ObjectStorage OVH

Release : 2.0 Last update Dependency size Repo size Downloads License

Simple library to use OVH Public Cloud Object Storage.

Create and manage containers (public, private or static), add and manage objects in OVH Public Cloud (OpenStack).

Based on developer.openstack.org official documentation.

A complete JSDoc is available here .

⚠️ Warning : This version introduces some breakpoints, if you want to use this version, it's recommended to refactoring all your projects.
To use the previous version (V1), go on #v1.0.x branch.

🎉 Features

  • Get account details and manage metas (create, update, has, get or delete),
  • Create, list, get details or delete containers,
  • Check if object exist in a container,
  • Define metas (create, update, has, get or delete) for container,
  • List files on a container,
  • Import object on container
  • Download or get content of object,
  • Get temporary download link object,
  • Delete all object in containers,
  • Program automatic delete of an object on datetime or after seconds,
  • Manage metas on objects (create, update, has, get or delete).

📍 Install via npm

npm install node-ovh-objectstorage --save

🧮 Examples :

Authentification

Requesting a token to make operations on container.

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  username: '******',
  password: '******',
  authURL: 'https://auth.cloud.ovh.net/v3/auth',
  tenantId: '******',
  region: 'SBG',
  // key is optional
  key: "Temporary key (optional), for generate temporary download link", 
  // options is optional
  options: {
    slugify : true, // true, by default 
    check_exists: true, // true, by default
  }
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();
  } catch (e) {
    // throw e
  }
})();

Account

Available methods :

  • all() : Object of account details and list containers (Promise)
  • details() : Object (Promise)
  • containers() : Array of each container object (Promise)
  • generateKey() : Array with key and header response object (Promise)
  • metas()
    • create( container, key, value ) : Header response (Promise)
    • create_with_result( container, key, value ) : Boolean (Promise)
    • update( container, key, value ) : Header response (Promise)
    • update_with_result( container, key, value ) : Boolean (Promise)
    • delete( container, key ) : Header response (Promise)
    • delete_with_result( container, key ) : Boolean (Promise)
    • all( container ) : Object of metas (Promise)
    • has( container, key ) : Boolean (Promise)
    • get( container, key ) : String or null (Promise)

Get account details and list containers

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    // account details and list containers
    await storage.account().all();

    // or only account details
    await storage.account().details();

    // or only containers list
    await storage.account().containers();
  } catch (e) {
    // throw e
  }
})();

Generate key for download file as temporary url

ℹ️️ You can define the key directly in the configuration and not generate a key with this method. The key must be in the Hexadecimal SHA1 format.

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    // account details and list containers
    let {key, headers} = await storage.account().generateKey();
    
    // IMPORTANT : you can save key in the configuration to avoid regenerating it and also avoid losing the temporary links already generated.
    
  } catch (e) {
    // throw e
  }
})();

Meta information of account

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    // list all metas of container
    await storage.account().metas().all("assets");

    // create a new meta author
    await storage.account().metas().create("assets", "author", "me");

    // retrieve value of author meta
    console.log(await storage.account().metas().get("assets", "author"));

    // update value of author meta
    await storage.account().metas().update("assets", "author", 'unknown');

    // if meta author exist on container name "assets", remove it
    if (await storage.account().metas().has("assets", "author"))
      await storage.account().metas().delete("assets", "author");
  } catch (e) {
    // throw e
  }
})();

Containers

Available methods :

  • create( container, types, web_content_pages ) : Header response (Promise)
  • create_with_result( container, types, web_content_pages ) : Boolean
  • list( container ) : Object (Promise)
  • exist( container ) : Boolean (Promise)
  • info( container ) : Header response (Promise)
  • delete_objects( container ) : Object (Promise)
  • delete_objects_with_result( container ) : Boolean (Promise)
  • delete( container, force=false ) : Header response (Promise)
  • delete_with_result( container, force=false ) : Boolean (Promise)
  • metas()
    • create( container, key, value ) : Header response (Promise)
    • create_with_result( container, key, value ) : Boolean (Promise)
    • update( container, key, value ) : Header response (Promise)
    • update_with_result( container, key, value ) : Boolean (Promise)
    • delete( container, key ) : Header response (Promise)
    • delete_with_result( container, key ) : Boolean (Promise)
    • all( container ) : Object of metas (Promise)
    • has( container, key ) : Boolean (Promise)
    • get( container, key ) : String or null (Promise)

Create

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.containers().create("private-files");
    await storage.containers().create("assets", "static", {
      index: 'index.html',
      css: 'style.css',
      error: 'error.html',
    });
    // or with boolean result
    console.log(await storage.containers().create_with_result("images", "public"));
  } catch (e) {
    // throw e
  }
})();

Delete

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.containers().delete("assets");
    // or with boolean result
    console.log(await storage.containers().delete_with_result("images"));

  } catch (e) {
    // throw e
  }
})();

Delete (container and all objects inside)

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.containers().delete("assets", true);
    // or with boolean result
    console.log(await storage.containers().delete_with_result("images", true));

  } catch (e) {
    // throw e
  }
})();

Remove all object in container

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.containers().delete_objects("assets");
    // or with boolean result
    console.log(await storage.containers().delete_objects_with_result("images"));

  } catch (e) {
    // throw e
  }
})();

List all object in container

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    let objects = await storage.containers().list("assets");
    console.log(objects);
  } catch (e) {
    // throw e
  }
})();

Check if container exist

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    if (await storage.containers().exist("assets"))
      console.log("Assets container exist !");
  } catch (e) {
    // throw e
  }
})();

Details about container

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    let details = await storage.containers().info("assets");
    console.log(details);
  } catch (e) {
    // throw e
  }
})();

Meta information of container

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    // list all metas of container
    await storage.containers().metas().all("assets");

    // create a new meta author
    await storage.containers().metas().create("assets", "author", "me");

    // retrieve value of author meta
    console.log(await storage.containers().metas().get("assets", "author"));

    // update value of author meta
    await storage.containers().metas().update("assets", "author", 'unknown');

    // if meta author exist on container name "assets", remove it
    if (await storage.containers().metas().has("assets", "author"))
      await storage.containers().metas().delete("assets", "author");
  } catch (e) {
    // throw e
  }
})();

Objects

  • save( file, path ) : Header response (Promise)
  • save_with_result( file, path ) : Header response (Promise)
  • set( file, path ) : Header response (Promise)
  • set_with_result( file, path ) : Header response (Promise)
  • exist( path ) : Boolean (Promise)
  • info( path ) : Header response (Promise)
  • get( path ) : Object with content and header response (Promise)
  • getTempLink( path, validityDurationInSeconds, checkContainer = true ) : Temporary download URL (Promise)
  • download( path, pathLocal ) : Boolean (Promise)
  • copy( pathOrigin, pathToPaste ) : Header response (Promise)
  • copy_with_result() : Boolean (Promise)
  • delete( path, checkContainer = true ) : Header response (Promise)
  • delete_with_result( path, checkContainer = true ) : Boolean (Promise)
  • deletes( path, checkContainer = true ) : Header response of all deleted files (Promise)
  • deletes_with_result( path, checkContainer = true ) : Boolean (Promise)
  • expire_at( path, expire_date ) : Header response (Promise)
  • expire_at_with_result( path, expire_date ) : Boolean (Promise)
  • expire_after( path, delete_seconds ) : Header response (Promise)
  • expire_after_with_result( path, delete_seconds ) : Boolean (Promise)
  • metas()
    • create( path, key, value ) : Header response (Promise)
    • create_with_result( path, key, value ) : Boolean (Promise)
    • update( path, key, value ) : Header response (Promise)
    • update_with_result( path, key, value ) : Boolean (Promise)
    • delete( path, key ) : Header response (Promise)
    • delete_with_result( path, key ) : Boolean (Promise)
    • all( path ) : Object of metas (Promise)
    • has( path, key ) : Boolean (Promise)
    • get( path, key ) : String or null (Promise)

Download object

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().download("/assets/IMG_1145.jpg", "./IMG_1145.jpg")
  } catch (e) {
    // throw e
  }
})();

Get object content

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    let rqt = await storage.objects().get("/assets/IMG_1145.jpg");
    console.log(rqt.content);
  } catch (e) {
    // throw e
  }
})();

Get temporary download object url

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.account().generateKey(); // if you have not defined a key in the configurations

    let url = await storage.objects().getTempLink("/assets/IMG_1145.jpg", 60 * 60); // expire after 1h
    console.log(url);
  } catch (e) {
    // throw e
  }
})();

Add object

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().save("./IMG_1145.jpg", "/assets/IMG_1145.jpg")
    // or with boolean result
    console.log(await storage.objects().save_with_result("./IMG_1145.jpg", "/assets/IMG_1145.jpg"))
  } catch (e) {
    // throw e
  }
})();

Clone object

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().copy("/assets/IMG_1145.jpg", "/assets/IMG_1145_2.jpg")
    // or with boolean result
    console.log(await storage.objects().copy_with_result("/assets/IMG_1145.jpg", "/assets/IMG_1145_2.jpg"))
  } catch (e) {
    // throw e
  }
})();

Delete object

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().delete("/assets/IMG_1145_2.jpg")
    // or with boolean result
    console.log(await storage.objects().delete_with_result("/assets/IMG_1145_2.jpg"))
  } catch (e) {
    // throw e
  }
})();

Details about object

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    let details = await storage.objects().info("/assets/IMG_1145.jpg")
    console.log(details);
  } catch (e) {
    // throw e
  }
})();

Programming object expiration at datetime (need momentjs)

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().expire_at("/assets/IMG_1145.jpg", moment().add("7", "days"))
    // or with boolean result
    console.log(await storage.objects().expire_at_with_result("/assets/IMG_1145.jpg", moment().add("7", "days")));
  } catch (e) {
    // throw e
  }
})();

Programming object expiration after seconds

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    await storage.objects().expire_after("/assets/IMG_1145.jpg", 180)
    // or with boolean result
    console.log(await storage.objects().expire_after_with_result("/assets/IMG_1145.jpg", 180));
  } catch (e) {
    // throw e
  }
})();

Meta information of container

var OVHStorage = require('node-ovh-objectstorage');
var config = {
  ...
};

(async () => {
  try {
    let storage = new OVHStorage(config);
    await storage.connection();

    // list all metas of object
    await storage.objects().metas().all("/assets/IMG_1145.jpg");

    // create a new meta author
    await storage.objects().metas().create("/assets/IMG_1145.jpg", "author", "me");

    // retrieve value of author meta
    console.log(await storage.objects().metas().get("/assets/IMG_1145.jpg", "author"));

    // update value of author meta
    await storage.objects().metas().update("/assets/IMG_1145.jpg", "author", 'unknown');

    // if meta author exist on object, remove it
    if (await storage.objects().metas().has("/assets/IMG_1145.jpg", "author"))
      await storage.objects().metas().delete("/assets/IMG_1145.jpg", "author");
  } catch (e) {
    // throw e
  }
})();

🧾 License

Creative Commons Attribution Share Alike 4.0 International