Knox S3 Client

0.9.2 · abandoned · verified Tue Apr 21

Knox is an Amazon S3 client library for Node.js, last updated in 2015 with version 0.9.3. It provided a familiar, low-level HTTP-client-like API for S3 operations such as `get()`, `put()`, and streaming uploads/downloads. At the time, it offered convenience methods like `putFile()` and `putStream()` for common file operations. Designed for Node.js environments from version 0.8 upwards, it was a common choice for S3 integration before the widespread adoption of the official AWS SDK v2/v3. However, the project is no longer maintained, with its last commit over eight years ago, and it is now considered abandoned. Users are strongly advised to migrate to the official AWS SDK for JavaScript for any new or existing S3 interactions due to security and compatibility concerns with unmaintained software.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the Knox client and upload a local file to S3 using the `putFile` method, including setting ACLs.

const knox = require('knox');
const fs = require('fs');
const path = require('path');

// Ensure you set these environment variables or replace with actual credentials
const S3_KEY = process.env.AWS_ACCESS_KEY_ID ?? 'YOUR_AWS_ACCESS_KEY_ID';
const S3_SECRET = process.env.AWS_SECRET_ACCESS_KEY ?? 'YOUR_AWS_SECRET_ACCESS_KEY';
const S3_BUCKET = process.env.AWS_BUCKET_NAME ?? 'your-bucket-name';

if (!S3_KEY || !S3_SECRET || !S3_BUCKET) {
  console.error('AWS credentials and bucket name must be set via environment variables or directly.');
  process.exit(1);
}

const client = knox.createClient({
  key: S3_KEY,
  secret: S3_SECRET,
  bucket: S3_BUCKET,
  region: 'us-east-1' // Specify your S3 region
});

const testFilePath = path.join(__dirname, 'test.txt');
fs.writeFileSync(testFilePath, 'Hello, Knox! This is a test file.');

// Example: Upload a file using putFile
client.putFile(testFilePath, '/uploads/test.txt', { 'x-amz-acl': 'public-read' }, (err, res) => {
  if (err) {
    console.error('Error uploading file:', err);
    return;
  }
  if (res && res.statusCode === 200) {
    console.log(`File uploaded successfully to: ${res.req.url}`);
    // Always either do something with `res` or at least call `res.resume()`.
    res.resume();
  } else {
    console.log(`File upload failed with status code: ${res ? res.statusCode : 'N/A'}`);
    if (res) res.resume();
  }

  // Clean up the local test file
  fs.unlinkSync(testFilePath);
});

view raw JSON →