JavaScript 快速入門指南
適用於 Amazon S3 相容雲端儲存的 MinIO JavaScript 程式庫 
MinIO JavaScript 用戶端 SDK 提供高階 API,用於存取任何 Amazon S3 相容的物件儲存伺服器。
本指南將示範如何安裝用戶端 SDK 並執行範例 JavaScript 程式。如需完整的 API 和範例清單,請參閱 JavaScript 用戶端 API 參考 文件。
本文件假設您具有可運作的 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 用戶端
需要下列參數才能連線到 MinIO 物件儲存伺服器
參數 |
描述 |
---|---|
|
物件儲存服務的主機名稱。 |
|
TCP/IP 連接埠號碼。選用,HTTP 預設為 |
|
S3 服務中帳戶的存取金鑰(使用者 ID)。 |
|
S3 服務中帳戶的密鑰(密碼)。 |
|
選用,設定為 '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',
})
快速入門範例 - 檔案上傳器
此範例會連線到物件儲存伺服器、建立 Bucket,並將檔案上傳到 Bucket。它使用 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 參考請見此處