MinIO: Connecting Node.js to MinIO

This documentation is part of the Getting started guide. You can view the complete guide here: How to begin using MinIO for S3-compatible object storage.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use MinIO Object Storage solution that provides a host of benefits, including:

  • Unlimited transfers.
  • Simple, predictive, and transparent pricing.
  • Customizable domain name secured with HTTPS (for example, https://object-storage.your-company.com).
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private and dedicated VM.
  • Available in 🇪🇺 Europe and 🇺🇸 USA.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's MinIO Object Storage hosting solution!

There are two methods to connect Node.js applications to MinIO.

First, install the AWS SDK package for Node.js:

npm install aws-sdk

Below is an example that shows how to connect to MinIO using the AWS SDK:

const process = require('process');
const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  accessKeyId: 'YOUR_ACCESS_KEY',
  secretAccessKey: 'YOUR_SECRET_KEY',
  endpoint: 'https://<XXXXXX>.stackhero-network.com',
  s3ForcePathStyle: true, // required with MinIO
  signatureVersion: 'v4'
});

// putObject operation
s3.putObject(
  { Bucket: 'testbucket', Key: 'testobject', Body: 'Hello from MinIO!' },
  (err, data) => {
    if (err)
      console.log(err);
    else
      console.log('Successfully uploaded data to testbucket/testobject');
  }
);

// getObject operation
const file = require('fs').createWriteStream('/tmp/mykey');
s3.getObject({ Bucket: 'testbucket', Key: 'testobject' })
  .on('httpData', chunk => file.write(chunk))
  .on('httpDone', () => file.end())
  .send();

For further details, visit the MinIO Node.js documentation.

If you prefer using the MinIO SDK for Node.js, install it using npm:

npm install minio

Here is an example of how to connect to MinIO using the MinIO SDK for Node.js:

const process = require('process');
const Minio = require('minio');

// Instantiate the MinIO client with the endpoint and access keys
const minioClient = new Minio.Client({
  endpoint: '<XXXXXX>.stackhero-network.com',
  port: 443,
  useSSL: true,
  accessKeyId: 'YOUR_ACCESS_KEY',
  secretAccessKey: 'YOUR_SECRET_KEY',
});

// File to be uploaded
const file = '/tmp/photos-europe.tar';

// Create a bucket called 'europetrip'
minioClient.makeBucket(
  'europetrip',
  'us-east-1',
  err => {
    if (err) return console.log(err);

    console.log('Bucket created successfully in "us-east-1".');

    const metaData = {
      'Content-Type': 'application/octet-stream',
      'X-Amz-Meta-Testing': 1234,
      'example': 5678
    };
    // Upload the file to the bucket 'europetrip' using fPutObject API
    minioClient.fPutObject(
      'europetrip',
      'photos-europe.tar',
      file,
      metaData,
      (err, etag) => {
        if (err) return console.log(err);
        console.log('File uploaded successfully.');
      }
    );
  }
);

For more information, see the MinIO Node.js SDK documentation.