notes.dt.in.th

The Minio JavaScript library can be used to talk with S3-compatible storage services, such as Linode Object Storage, DigitalOcean Spaces, or Oracle Cloud’s Object Storage.

import Minio from 'minio'

const minioClient = new Minio.Client({
  endPoint: 'ap-south-1.linodeobjects.com',
  useSSL: true,
  accessKey: process.env.AWS_ACCESS_KEY_ID,
  secretKey: process.env.AWS_SECRET_ACCESS_KEY,
})

Also install @types/minio for TypeScript typings.

endPoint

// Oracle Cloud
endPoint = `${namespace}.compat.objectstorage.${region}.oraclecloud.com`

// Linode
endPoint = `${region}.linodeobjects.com`

Uploading objects

await minioClient.putObject(bucketName, name, data, {
  // Headers, e.g.
  // 'Content-Type': 'application/json',
  // 'Cache-Control': 'max-age=300',
})

Downloading objects

export async function loadObject(name) {
  const stream = await minioClient.getObject(bucketName, name)
  const buffers = []
  for await (const chunk of stream) {
    buffers.push(chunk)
  }
  return Buffer.concat(buffers)
}