akamai-http-api

Akamai NetStorage HTTP API for the Node.js

node-akamai-http-api

Akamai NetStorage HTTP API for the Node.js

"dependencies": {
  "akamai-http-api": "0.3.*"
}

npm update

var akamai = require('akamai-http-api');
akamai.setConfig({
  keyName: 'keyName',
  key: 'aLongString',
  host: 'changeme.akamaihd.net',
  ssl: true,
  verbose: false
});
  1. You have to enable the netstorage HTTP API access using the control.akamai.com website
  2. Ensure there are no more than 15 operations/second on netstorage, otherwise you can expect netstorage to serve 500 errors.
var fs = require('fs'),
  stream = fs.createReadStream('cool/file.jpg');
akamai.upload(stream, '/12345/MyFolder/MyFile.jpg', function (errdata) {});
var fs = require('fs'),
  stream = fs.createWriteStream('cool/file_download.jpg');
akamai.download(pathRemoteFile, stream, function (errdata) {});
akamai.mtime('/12345/MyFolder/MyFile.jpg', new Date(), function (errdata) {});
akamai.du('/12345/MyFolder', function (errdata) {});
akamai.dir('/12345/MyFolder', function (errdata) {});
akamai.stat('/12345/MyFolder', function (errdata) {});
akamai.delete('/12345/MyFolder/MyFile.jpg', function (errdata) {});
akamai.mkdir('/12345/MyFolder', function (errdata) {});
akamai.rmdir('/12345/MyFolder', function (errdata) {});
akamai.rename('/12345/MyFile.jpg', '/12345/MyFileNew.jpg', function (errdata) {});
akamai.symlink('/12345/MyFile.jpg', '/12345/MyFileSymlink.jpg', function (errdata) {});
akamai.fileExists('/12345/MyFile.jpg', function (errboolFlag) {});

For the communication netstorage HTTP API uses HTTP codes. Hence, a number of methods may trigger an exception. For example mkdir in case the target already exists. Or symlink in case the target doesn't exist.

To handle these exceptions the err object has an abnormal code attribute.

akamai.mkdir('...', function (errdata) {
  if (err.code === 409) { } // it already exists 
  if (err.message.indexOf(409) !== -1) // the same 
});
var akamai = require('akamai-http-api'),
  _ = require('lodash'),
  myAkamai = Object.create(akamai);
 
// custom headers for the upload function 
myAkamai.upload = function (streampathcustHeaderscb) {
  var options = {
    request: {method: 'put'},
    headers: _.extend({action: 'upload', 'upload-type': 'binary'}, custHeaders)
  };
  stream.pipe(this.getRequestObject(path, options, cb));
  return this;
};
 
// quick-delete function (you should enable it first!) 
myAkamai.quickDelete = function (pathcb) {
  var options = {
    request: {method: 'put'},
    headers: {action: 'quick-delete', 'quick-delete': 'imreallyreallysure'}
  };
  this.getRequestObject(path, options, cb);
  return this;
};
 
// exporting outside 
module.exports = myAkamai;
# modify test/akamai.js#15 first 
[sudo] docker build -t node-akamai-http-api .
[sudo] docker run -ti --rm node-akamai-http-api

The MIT License (MIT)

Copyright (c) 2013-2015 Kanstantsin Kamkou

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.