快速开始
MOS Javascript SDK是Simple Storage Service(又称S3)客户端,可对任何Amazon S3兼容的对象存储服务执行桶(bucket)和对象(object)操作。
从 NPM 安装¶
npm install --save minio
从源码安装¶
git clone https://github.com/minio/minio-js
cd minio-js
npm install
npm install -g
初始化 MOS 客户端¶
连接到对象存储服务器需要三个参数。
参数 | 描述 |
---|---|
endpoint | S3服务的URL。 |
port | S3服务的端口。 |
accessKey | S3服务中帐户的访问密钥(也称为用户ID)。 |
secretKey | S3服务中帐户的秘密密钥(也称为密码)。 |
useSSL | 是否使用HTTPS安全访问 |
var Minio = require('minio')
var minioClient = new Minio.Client({
endPoint: 'play.min.io',
port: 9000,
useSSL: true,
accessKey: 'Q3A***********3P2F',
secretKey: 'zuf+t************************KYY3TG'
});
示例 - File Uploader¶
此示例程序连接到对象存储服务器,在服务器上创建一个桶(bucket),然后将文件上传到该桶(bucket)。
file-uploader.js¶
var Minio = require('minio')
// Instantiate the minio client with the endpoint
// and access keys as shown below.
var minioClient = new Minio.Client({
endPoint: 'play.min.io',
port: 9000,
useSSL: true,
accessKey: 'Q3A***********3P2F',
secretKey: 'zuf+t************************KYY3TG'
});
// File that needs to be uploaded.
var file = '/tmp/photos-europe.tar'
// Make a bucket called europetrip.
minioClient.makeBucket('europetrip', 'us-east-1', function(err) {
if (err) return console.log(err)
console.log('Bucket created successfully in "us-east-1".')
var metaData = {
'Content-Type': 'application/octet-stream',
'X-Amz-Meta-Testing': 1234,
'example': 5678
}
// Using fPutObject API upload your file to the bucket europetrip.
minioClient.fPutObject('europetrip', 'photos-europe.tar', file, metaData, function(err, etag) {
if (err) return console.log(err)
console.log('File uploaded successfully.')
});
});
运行 file-uploader¶
node file-uploader.js
Bucket created successfully in "us-east-1".
mc ls play/europetrip/
[2016-05-25 23:49:50 PDT] 17MiB photos-europe.tar