Akamai NetStorage HTTP API for Node.js

0.6.1 · maintenance · verified Tue Apr 21

This package provides an unofficial Node.js client for interacting with the Akamai NetStorage HTTP API. It is currently at version `0.6.1` and primarily focuses on maintenance, addressing dependency updates and security vulnerabilities rather than new feature development. The library differentiates itself by providing a seemingly more robust and actively maintained alternative to Akamai's official Node.js NetStorageKit, which the README notes as having "suspicious quality." It offers both stream-based 'Advanced' APIs for uploading and downloading files, and 'Basic' APIs for common file and directory operations like `du`, `dir`, `stat`, `delete`, `mkdir`, `rmdir`, `rename`, and `symlink`. It includes helpers like `fileExists` and provides explicit handling for HTTP response codes in error objects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `akamai-http-api` client using configuration from environment variables and then perform a basic file upload to Akamai NetStorage via a readable stream. It includes local file creation and cleanup.

const akamai = require('akamai-http-api');
const fs = require('fs');
const path = require('path');

// Ensure these environment variables are set or provide direct values
akamai.setConfig({
  keyName: process.env.AKAMAI_KEY_NAME ?? 'your-akamai-keyname',
  key: process.env.AKAMAI_KEY ?? 'your-akamai-secret-key-long-string',
  host: process.env.AKAMAI_HOST ?? 'your-netstorage-hostname.akamaihd.net',
  ssl: true, // Recommended for production
  verbose: false,
  request: {
    timeout: 30000 // 30 seconds timeout (default is 20s)
  }
});

const localTempDir = './temp_akamai_uploads';
if (!fs.existsSync(localTempDir)) {
  fs.mkdirSync(localTempDir);
}
const localFilePath = path.join(localTempDir, 'example-upload.txt');
const remoteFilePath = '/12345/MyFolder/example-upload.txt'; // Replace 12345 with your CP code

// Create a dummy file for upload
fs.writeFileSync(localFilePath, 'Hello Akamai NetStorage from node-akamai-http-api!');
console.log(`Created local file: ${localFilePath}`);

const uploadStream = fs.createReadStream(localFilePath);

akamai.upload(uploadStream, remoteFilePath, function (err, data) {
  if (err) {
    console.error('Akamai upload failed:', err.message || err);
    // Clean up even on error
    fs.unlinkSync(localFilePath);
    fs.rmdirSync(localTempDir);
    return;
  }
  console.log('Akamai upload successful:', data);

  // Clean up the dummy file and directory
  fs.unlinkSync(localFilePath);
  fs.rmdirSync(localTempDir);
  console.log(`Cleaned up local file: ${localFilePath}`);
});

view raw JSON →