netstorageapi
Advanced tools
Comparing version 1.0.1 to 1.1.2
@@ -42,22 +42,16 @@ // Original author: Astin Choi <achoi@akamai.com> | ||
dir(ns_path, callback) { | ||
return this.requestor.makeRequest({ | ||
action: 'dir&format=xml', | ||
method: 'GET', | ||
path: ns_path | ||
}, | ||
callback) | ||
dir(opts, callback) { | ||
try { | ||
return this.requestor.makeRequest(this.buildRequestOptions(this.dir.name, opts), callback) | ||
} catch (err) { | ||
return callback(err, null, null) | ||
} | ||
} | ||
list(ns_path, actionHeaders, callback) { | ||
if (!actionHeaders['max_entries'] > 0) { | ||
return callback(new Error('[Netstorage Error] You should supply the supplimental action headers object as a second argument'), null, null) | ||
list(opts, callback) { | ||
try { | ||
return this.requestor.makeRequest(this.buildRequestOptions(this.list.name, opts), callback) | ||
} catch (err) { | ||
return callback(err, null, null) | ||
} | ||
this.actionHeaders = `list&format=xml&max_entries=${actionHeaders['max_entries']}` | ||
return this.requestor.makeRequest({ | ||
action: this.actionHeaders, | ||
method: 'GET', | ||
path: ns_path | ||
}, | ||
callback) | ||
} | ||
@@ -190,4 +184,42 @@ | ||
buildRequestOptions(method, opts) { | ||
var method = method | ||
var opts = opts | ||
var baseActions = `${method}&format=xml` | ||
if (typeof opts === 'object') { | ||
if (opts.path) { | ||
if (opts.actions instanceof Object && Object.keys(opts.actions).length > 0) { | ||
return { | ||
action: baseActions+this.buildRequestActions(opts.actions), | ||
method: 'GET', | ||
path: opts.path | ||
} | ||
} else { | ||
throw new Error('[Netstorage Error] If an options object is passed, it must contain an "actions" object with key/value pairs for each action option') | ||
} | ||
} else { | ||
throw new Error('[Netstorage Error] If an options object is passed, it must contain a "path" key/value pair') | ||
} | ||
} else if (typeof opts === 'string') { | ||
return { | ||
action: baseActions, | ||
method: 'GET', | ||
path: opts | ||
} | ||
} else { | ||
throw new Error('[Netstorage Error] Options must be either a string path, or an object containing all desired options') | ||
} | ||
} | ||
buildRequestActions(actions) { | ||
var actions = actions | ||
var parsedActions = '' | ||
Object.keys(actions).forEach((action) => { | ||
parsedActions += `&${action}=${actions[action]}` | ||
}) | ||
return parsedActions | ||
} | ||
} | ||
module.exports = Netstorage |
{ | ||
"name": "netstorageapi", | ||
"version": "1.0.1", | ||
"version": "1.1.2", | ||
"description": "Akamai Netstorage API for Node.js", | ||
@@ -20,3 +20,5 @@ "main": "./lib/netstorage.js", | ||
"open", | ||
"api" | ||
"api", | ||
"filestore", | ||
"objectstore" | ||
], | ||
@@ -23,0 +25,0 @@ "author": "Astin Choi <asciineo@gmail.com>", |
353
README.md
@@ -1,3 +0,2 @@ | ||
NetstorageAPI: Akamai Netstorage API for Node.js | ||
================================================ | ||
# NetstorageAPI: Akamai Netstorage API for Node.js | ||
@@ -12,9 +11,28 @@ [](https://badge.fury.io/js/netstorageapi) | ||
# Table of Contents | ||
* [Installation](#installation) | ||
* [Example](#example) | ||
* [Methods](#methods) | ||
* [delete](#delete) | ||
* [dir](#dir) | ||
* [list](#list) | ||
* [download](#download) | ||
* [du](#du) | ||
* [mkdir](#mkdir) | ||
* [mtime](#mtime) | ||
* [quick_delete](#quick_delete) | ||
* [rename](#rename) | ||
* [rmdir](#rmdir) | ||
* [stat](#stat) | ||
* [symlink](#symlink) | ||
* [upload](#upload) | ||
* [Testing](#testing) | ||
* [Author](#author) | ||
* [License](#license) | ||
Installation | ||
------------ | ||
# Installation | ||
To install Netstorage API for Node.js: | ||
```bash | ||
```Shell | ||
$ npm install --save netstorageapi | ||
@@ -24,119 +42,254 @@ ``` | ||
Example | ||
------- | ||
# Example | ||
```javascript | ||
> const Netstorage = require('netstorageapi') | ||
> | ||
> const config = { hostname: 'astinobj-nsu.akamaihd.net', keyName: 'astinobj', key: 'xxxxxxxxxx', cpCode: '407617', ssl: false } | ||
> // Don't expose KEY on your public repository. | ||
> // Default SSL value is FALSE | ||
> var ns = new Netstorage(config) | ||
> var local_source = 'hello.txt' | ||
> var netstorage_destination = `/${config.cpCode}/hello.txt` // or `/${config.cpCode}/` is same. | ||
> | ||
> ns.upload(local_source, netstorage_destination, (error, response, body) => { | ||
... if (error) { // errors other than http response codes | ||
... console.log(`Got error: ${error.message}`) | ||
... } | ||
... if (response.statusCode == 200) { | ||
... console.log(body) | ||
... } | ||
... }); | ||
```Javascript | ||
const Netstorage = require('netstorageapi') | ||
// Defaults: SSL: false | ||
const config = { | ||
hostname: 'astinobj-nsu.akamaihd.net', | ||
keyName: 'astinobj', | ||
key: 'xxxxxxxxxx', | ||
cpCode: '407617', | ||
ssl: false | ||
} | ||
// Don't expose KEY on your public repository. | ||
const ns = new Netstorage(config) | ||
const local_source = 'hello.txt' | ||
// or `/${config.cpCode}/` will asume the destination filename is the same as the source | ||
const netstorage_destination = `/${config.cpCode}/hello.txt` | ||
ns.upload(local_source, netstorage_destination, (error, response, body) => { | ||
if (error) { // errors other than http response codes | ||
console.log(`Got error: ${error.message}`) | ||
} | ||
if (response.statusCode == 200) { | ||
console.log(body) | ||
} | ||
}); | ||
{ message: 'Request Processed.' } | ||
> | ||
``` | ||
Methods | ||
------- | ||
# Methods | ||
```javascript | ||
> function callback(error, response, body) { /* do something */ } | ||
> | ||
> ns.delete(NETSTORAGE_PATH, callback); | ||
> ns.dir(NETSTORAGE_PATH, callback); | ||
> ns.list(NETSTORAGE_PATH, ACTIONS_OBJ, callback); | ||
> ns.download(NETSTORAGE_SOURCE, LOCAL_DESTINATION, callback); | ||
> ns.du(NETSTORAGE_PATH, callback); | ||
> ns.mkdir(`#{NETSTORAGE_PATH}/#{DIRECTORY_NAME}`, callback); | ||
> ns.mtime(NETSTORAGE_PATH, Math.floor(Date.now()/1000), callback); | ||
> ns.quick_delete(NETSTORAGE_DIR, callback); // needs to be enabled on the CP Code | ||
> ns.rename(NETSTORAGE_TARGET, NETSTORAGE_DESTINATION, callback); | ||
> ns.rmdir(NETSTORAGE_DIR, callback); // remove empty direcoty | ||
> ns.stat(NETSTORAGE_PATH, callback); | ||
> ns.symlink(NETSTORAGE_TARGET, NETSTORAGE_DESTINATION, callback); | ||
> ns.upload(LOCAL_SOURCE, NETSTORAGE_DESTINATION, callback); | ||
> | ||
> // INFO: can "upload" Only a single file, not directory. | ||
> // WARN: can raise FILE related error in "download" and "upload", | ||
> // see error object in callback. | ||
* [delete](#delete) | ||
* [dir](#dir) | ||
* [list](#list) | ||
* [download](#download) | ||
* [du](#du) | ||
* [mkdir](#mkdir) | ||
* [mtime](#mtime) | ||
* [quick_delete](#quick_delete) | ||
* [rename](#rename) | ||
* [rmdir](#rmdir) | ||
* [stat](#stat) | ||
* [symlink](#symlink) | ||
* [upload](#upload) | ||
### delete | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.delete(NETSTORAGE_PATH, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path for the file, not the directory | | ||
Test | ||
---- | ||
You can test all above methods with [Unit Test Script](https://github.com/akamai-open/NetStorageKit-Node/blob/master/test/test-netstorage.js). you should configure [api-config.json](https://github.com/akamai-open/NetStorageKit-Node/blob/master/test/api-config.json.example). It uses [Mocha](https://mochajs.org/) for the test: | ||
### dir | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.dir(NETSTORAGE_PATH|OPTIONS, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path for the directory | | ||
| `OPTIONS` | *object* | JSON object containing options for the dir method | | ||
- **Valid Options**: | ||
```Javascript | ||
{ path: '/your/path', | ||
actions: { | ||
max_entries: integer, | ||
start: '/start/path', | ||
end: '/end/path/', | ||
prefix: 'object-prefix', | ||
slash: 'both' | ||
} | ||
} | ||
``` | ||
```bash | ||
$ npm install --global mocha | ||
... | ||
$ export TEST_MODE=LOCAL # use test/api-config.json | ||
$ mocha --no-timeouts test/test_netstorage.js | ||
### list | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.list(NETSTORAGE_PATH|OPTIONS, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
### Netstorage test ### | ||
ns.dir("/407617", callback); | ||
✓ should return 200 OK | ||
ns.list("/407617", { "max_entries": 5 }, callback); | ||
✓ should return 200 OK | ||
ns.mkdir("/407617/nst_1485516660306", callback); | ||
✓ should return 200 OK | ||
ns.upload("/Users/achoi/Projects/NetStorageKit-Node/test/nst_1485516660306.txt", "/407617/nst_1485516660306/nst_1485516660306.txt", callback); | ||
✓ should return 200 OK | ||
ns.du("/407617/nst_1485516660306", callback); | ||
✓ should return 200 OK | ||
ns.mtime("/407617/nst_1485516660306/nst_1485516660306.txt", 1485516660, callback); | ||
✓ should return 200 OK | ||
ns.stat("/407617/nst_1485516660306/nst_1485516660306.txt", callback); | ||
✓ should return 200 OK | ||
ns.symlink("/407617/nst_1485516660306/nst_1485516660306.txt", "/407617/nst_1485516660306/nst_1485516660306.txt_lnk", callback); | ||
✓ should return 200 OK | ||
ns.rename("/407617/nst_1485516660306/nst_1485516660306.txt", "/407617/nst_1485516660306/nst_1485516660306.txt_rename", callback); | ||
✓ should return 200 OK | ||
ns.download("/407617/nst_1485516660306/nst_1485516660306.txt_rename", callback); | ||
✓ should return 200 OK | ||
ns.delete("/407617/nst_1485516660306/nst_1485516660306.txt_rename", callback); | ||
✓ should return 200 OK | ||
ns.delete("/407617/nst_1485516660306/nst_1485516660306.txt_lnk", callback); | ||
✓ should return 200 OK | ||
ns.rmdir("/407617/nst_1485516660306", callback); | ||
✓ should return 200 OK | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path to the file/directory | | ||
| `OPTIONS` | *object* | JSON object containing options for the dir method | | ||
- **Valid Options**: | ||
```Javascript | ||
{ path: '/your/path', | ||
actions: { | ||
max_entries: integer, | ||
end: '/end/path/' | ||
} | ||
} | ||
``` | ||
### Error test ### | ||
ns.dir('invalid ns path', callback); | ||
✓ should get Error object | ||
ns.list('invalid ns path', { "max_entries": 5 }, callback); | ||
✓ should get Error object | ||
ns.list("/407617", { badObj: true }, callback); | ||
✓ should get Error object | ||
ns.upload("invalid local path", "/407617/nst_1485516660306/nst_1485516660306.txt" callback); | ||
✓ should get Error object | ||
ns.download("/123456/directory/", "/Users/achoi/Projects/NetStorageKit-Node/test/nst_1485516660306.txt" callback); | ||
✓ should get Error object | ||
### download | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.download(NETSTORAGE_SOURCE, LOCAL_DESTINATION, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_SOURCE` | *string* | Path to the file in NetStorage | | ||
| `LOCAL_DESTINATION` | *string* | Location on the local host to write the downloaded file to | | ||
18 passing (..s) | ||
### du | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.du(NETSTORAGE_PATH, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path to the file/directory | | ||
Author | ||
------ | ||
### mkdir | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.mkdir(DIRECTORY_NAME, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `DIRECTORY_NAME` | *string* | Full path to the directory you wish to create | | ||
### mtime | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.mtime(NETSTORAGE_PATH, UNIX_TIME, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path to the file/directory | | ||
| `UNIX_TIME` | integer | Unix time to set the mtime of the file to. Note that millisecond accuracy is not supported | | ||
### quick_delete | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.quick_delete(NETSTORAGE_DIR, callback(err, response, body)) // needs to be enabled on the CP Code | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_DIR` | *string* | full path to the directory you wish to delete| | ||
### rename | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.rename(NETSTORAGE_TARGET, NETSTORAGE_DESTINATION, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_TARGET` | *string* | full path to the original file/directory | | ||
| `NETSTORAGE_DESTINATION` | *string* | full path to the renamed file/directory | | ||
### rmdir | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.rmdir(NETSTORAGE_DIR, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_DIR` | *string* | full path to the empty object you wish to delete | | ||
### stat | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.stat(NETSTORAGE_PATH, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_PATH` | *string* | full path to the file/directory you wish to stat | | ||
### symlink | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.symlink(NETSTORAGE_TARGET, NETSTORAGE_DESTINATION, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `NETSTORAGE_TARGET` | *string* | full path of the symlink to create | | ||
| `NETSTORAGE_DESTINATION` | *string* | full path to file to symlink to | | ||
### upload | ||
######*[^ back to method list](#methods)* | ||
- **Syntax**: | ||
```Javascript | ||
ns.upload(LOCAL_SOURCE, NETSTORAGE_DESTINATION, callback(err, response, body)) | ||
``` | ||
- **Parameters**: | ||
| Name | Type | Description | | ||
| :---------- | :---------: | :------------------------------ | | ||
| `LOCAL_SOURCE` | *string* | Path to the local file you wish to upload | | ||
| `NETSTORAGE_DESTINATION` | *string* | Path to the location you wish to upload the file. Note that if you omit the actual filename, the source filename is used. You may only upload files using the API, not directories. | | ||
# Testing | ||
Unit tests for all of the above methods are executed via the [test script](https://github.com/akamai-open/NetStorageKit-Node/blob/master/test/test-netstorage.js). Prior to testing, create an api-config.json file in the test directory using the provided [example](https://github.com/akamai-open/NetStorageKit-Node/blob/master/test/api-config.json.example) for the required values. The excellent [Mocha](https://mochajs.org/) and [ChaiJS](http://chaijs.com) libraries are used for all tests: | ||
```Shell | ||
$ npm install | ||
$ export TEST_MODE=LOCAL # use test/api-config.json | ||
$ npm test | ||
``` | ||
# Author | ||
Astin Choi (achoi@akamai.com) | ||
# License | ||
License | ||
------- | ||
Copyright 2016 Akamai Technologies, Inc. All rights reserved. | ||
@@ -143,0 +296,0 @@ |
@@ -58,5 +58,5 @@ // Original author: Astin Choi <achoi@akamai.com> | ||
describe(`ns.list("/${config.cpCode}", { "max_entries": 5 }, callback);`, function() { | ||
describe(`ns.list({ path: "/${config.cpCode}", actions: { "max_entries": 5 } }, callback);`, function() { | ||
it('should return 200 OK', function(done) { | ||
ns.list(`/${config.cpCode}`, { 'max_entries': 5 }, (error, response, body) => { | ||
ns.list({ path: `/${config.cpCode}`, actions: { max_entries: 5 } }, (error, response, body) => { | ||
expect(response.statusCode).to.equal(200) | ||
@@ -189,5 +189,5 @@ expect(body.list.file.length).to.equal(5) | ||
describe(`ns.list('invalid ns path', { "max_entries": 5 }, callback);`, function() { | ||
describe('ns.list({ path: "INVALID NS PATH", actions: { max_entries: 5 } }, callback)', function() { | ||
it('should get Error object', function(done) { | ||
ns.list('Invalid ns path', { 'max_entries': 5 }, (error, response, body) => { | ||
ns.list({ path: "INVALID NS PATH", actions: { max_entries: 5 } }, (error, response, body) => { | ||
if (error) { | ||
@@ -202,8 +202,8 @@ expect(error).to.be.instanceof(Error) | ||
describe(`ns.list("/${config.cpCode}", { badObj: true }, callback);`, function() { | ||
describe(`ns.list({ path: "/${config.cpCode}" }, callback);`, function() { | ||
it('should get Error object', function(done) { | ||
ns.list(`/${config.cpCode}`, { 'max_entries': 5 }, (error, response, body) => { | ||
ns.list({ path: `/${config.cpCode}` }, (error, response, body) => { | ||
if (error) { | ||
expect(error).to.be.instanceof(Error) | ||
expect(error.message).to.equal('[Netstorage Error] Invalid netstorage path') | ||
expect(error.message).to.equal('[Netstorage Error] If an options object is passed, it must contain an "actions" object with key/value pairs for each action option') | ||
} | ||
@@ -210,0 +210,0 @@ done() |
Sorry, the diff of this file is not supported yet
43428
551
306