Akamai NetStorage HTTP API for Node.js
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
-
Error: Akamai API Error. Status: 409 Conflict
cause Attempting to create a directory (`mkdir`) or symlink (`symlink`) where a file or directory with the same name already exists at the target path.fixImplement checks (e.g., using `fileExists` or `stat`) before creating resources, or specifically handle the `err.code === 409` condition in your error callback. -
Error: read ECONNRESET
cause A network connection reset occurred. This can happen due to an unstable network, Akamai rate limiting abruptly closing the connection, or an incorrect host configuration causing the server to reject the connection.fixVerify your network stability, ensure the `host` is correctly configured, and check Akamai's rate limit guidance. Consider implementing retry logic with back-off. -
Error: ETIMEDOUT (or similar timeout error)
cause The HTTP request to Akamai NetStorage exceeded the configured `request.timeout` value before receiving a response. This could be due to network latency, large file transfers, or an unreachable host.fixCheck network connectivity and the correctness of the configured `host`. For large files, consider increasing the `request.timeout` value in `setConfig`.
Warnings
- breaking The core HTTP client library, 'request', which `akamai-http-api` depends on, is officially deprecated and no longer maintained. While this package updates its dependencies, continued reliance on 'request' could lead to future compatibility issues or unpatched security vulnerabilities in the underlying HTTP layer.
- gotcha Multiple security vulnerabilities in the 'lodash' dependency have been patched. To ensure your application is secure, it is critical to use `akamai-http-api` version `0.6.1` or higher.
- gotcha Akamai NetStorage has a soft rate limit of approximately 15 operations per second. Exceeding this limit can result in 500 series HTTP errors being returned by NetStorage, which are not automatically retried or handled by this client.
- gotcha Providing an incorrect `host` value in `setConfig` (e.g., a typo) will not immediately throw an error but will cause HTTP requests to hang until the connection timeout is reached (default 20 seconds).
- deprecated The configuration for HTTP request timeouts was introduced and later standardized under a nested `request` object. Direct top-level `timeout` options might be ignored or behave inconsistently in newer versions.
Install
-
npm install akamai-http-api -
yarn add akamai-http-api -
pnpm add akamai-http-api
Imports
- akamai
const akamai = require('akamai-http-api');import akamai from 'akamai-http-api';
- setConfig
akamai.setConfig({ /* config */ }); - upload
akamai.upload(stream, '/path/to/remote', callback);
Quickstart
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}`);
});