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 platform designed for reliability and speed. The main advantages include:
- Unlimited data transfers with no hidden fees.
- Simple, predictable, and transparent pricing.
- A custom domain name secured with HTTPS (for example, https://object-storage.your-company.com).
- Smooth, one-click updates with no manual intervention required.
- High performance and security on private, dedicated infrastructure.
- Available in both 🇪🇺 Europe and 🇺🇸 USA regions.
Simplify your workflows and save setup time: you can deploy Stackhero's MinIO Object Storage hosting in just 5 minutes.
There are two methods to connect Node.js applications to MinIO.
Connecting to MinIO with the AWS SDK
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.
Connecting to MinIO with the MinIO SDK
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.