JavaScript Quickstart Guide

适用于 Amazon S3 兼容对象存储的 MinIO JavaScript 库 Slack

NPM

MinIO JavaScript Client SDK 提供了高级 API,可用于访问任何兼容 Amazon S3 的对象存储服务器。

本指南介绍如何安装 client SDK 并执行一个 JavaScript 示例程序。 如需完整的 API 与示例列表,请参阅 JavaScript Client API Reference 文档。

本文档假定你已具备可用的 Node.js 开发环境,支持的 LTS 版本为 v16、v18 或 v20。

从 NPM 下载

npm install --save minio

从源码下载

git clone https://github.com/minio/minio-js
cd minio-js
npm install
npm run build
npm install -g

与 TypeScript 搭配使用

minio>7.1.0 已内置类型定义,不再需要 @types/minio

初始化 MinIO Client

连接到 MinIO 对象存储服务器需要以下参数:

ParameterDescription
endPoint对象存储服务的主机名。
portTCP/IP 端口号。可选;默认为 HTTP 的 80 和 HTTPS 的 443
accessKeyS3 服务中某个账户的访问密钥(用户 ID)。
secretKeyS3 服务中某个账户的 Secret Key(相当于密码)。
useSSL可选,将其设置为 true 以启用安全(HTTPS)访问。
import * as Minio from 'minio'

const minioClient = new Minio.Client({
  endPoint: 'play.min.io',
  port: 9000,
  useSSL: true,
  accessKey: 'Q3AM3UQ867SPQQA43P2F',
  secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
})

快速开始示例 - 文件上传器

该示例连接到对象存储服务器,创建一个存储桶,并将文件上传到该存储桶。 它使用 MinIO play 服务器,即位于 https://play.min.io 的公开 MinIO 集群。

play 服务器运行 MinIO 的最新稳定版本,可用于测试和开发。 本示例中展示的访问凭据对公众开放。 上传到 play 的所有数据都应视为公开且不受保护。

file-uploader.mjs

import * as Minio from 'minio'

// Instantiate the MinIO client with the object store service
// endpoint and an authorized user's credentials
// play.min.io is the MinIO public test cluster
const minioClient = new Minio.Client({
  endPoint: 'play.min.io',
  port: 9000,
  useSSL: true,
  accessKey: 'Q3AM3UQ867SPQQA43P2F',
  secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
})

// File to upload
const sourceFile = '/tmp/test-file.txt'

// Destination bucket
const bucket = 'js-test-bucket'

// Destination object name
const destinationObject = 'my-test-file.txt'

// Check if the bucket exists
// If it doesn't, create it
const exists = await minioClient.bucketExists(bucket)
if (exists) {
  console.log('Bucket ' + bucket + ' exists.')
} else {
  await minioClient.makeBucket(bucket, 'us-east-1')
  console.log('Bucket ' + bucket + ' created in "us-east-1".')
}

// Set the object metadata
var metaData = {
  'Content-Type': 'text/plain',
  'X-Amz-Meta-Testing': 1234,
  example: 5678,
}

// Upload the file with fPutObject
// If an object with the same name exists,
// it is updated with new data
await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData)
console.log('File ' + sourceFile + ' uploaded as object ' + destinationObject + ' in bucket ' + bucket)

运行文件上传器

node file-uploader.mjs
Bucket js-test-bucket created successfully in "us-east-1".
File /tmp/test-file.txt uploaded successfully as my-test-file.txt to bucket js-test-bucket

使用 mc 验证对象已创建:

mc ls play/js-test-bucket
[2023-11-10 17:52:20 UTC]  20KiB STANDARD my-test-file.txt

API 参考

完整的 API 参考可在此查看:

存储桶操作

文件对象操作

对象操作

Presigned 操作

存储桶通知操作

存储桶策略操作

示例

存储桶操作

文件对象操作

对象操作

Presigned 操作

存储桶通知操作

存储桶策略操作

自定义设置

深入了解

贡献

GitHub Workflow Status

Last modified August 2, 2026: init commit (8338d5b)

Portions of this page are adapted from the MinIO Object Storage Documentation, © 2020–Present MinIO, Inc., licensed under CC BY 4.0, converted and maintained by the Silo project. Attribution