dimer47 / node-ovh-objectstorage

Creative Commons Attribution Share Alike 4.0 International
12 stars 11 forks source link

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

📍 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 :

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

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

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