Skip to content

Latest commit

 

History

History
61 lines (41 loc) · 1.76 KB

aws-sdk-for-javascript-with-minio.md

File metadata and controls

61 lines (41 loc) · 1.76 KB

如何使用AWS SDK for Javascript操作MinIO Server Slack

本文我们将学习如何使用aws-sdk for Javascript操作MinIO Server。aws-sdk for Javascript是Javascript语言版本的官方AWS SDK。

1. 前提条件

这里下载并安装MinIO Server。

2. 安装

AWS Javascript SDK官方文档下载将安装aws-sdk for Javascript。

3. 示例

修改example.js文件中的endpoint,accessKeyId, secretAccessKeyBucket以及Object配置成你的本地配置。

下面的示例讲的是如何使用aws-sdk for Javascript从MinIO Server上putObject和getObject。

var AWS = require('aws-sdk');

var s3  = new AWS.S3({
          accessKeyId: 'YOUR-ACCESSKEYID' ,
          secretAccessKey: 'YOUR-SECRETACCESSKEY' ,
          endpoint: 'http://127.0.0.1:9000' ,
          s3ForcePathStyle: true,
          signatureVersion: 'v4'
});

// putObject操作

var params = {Bucket: 'testbucket', Key: 'testobject', Body: 'Hello from MinIO!!'};

s3.putObject(params, function(err, data) {
      if (err)
       console.log(err)
      else   
       console.log("Successfully uploaded data to testbucket/testobject");
});

// getObject操作

var params = {Bucket: 'testbucket', Key: 'testobject'};

var file = require('fs').createWriteStream('/tmp/mykey');

s3.getObject(params).
on('httpData', function(chunk) { file.write(chunk); }).
on('httpDone', function() { file.end(); }).
send();

4. 运行程序

node example.js
Successfully uploaded data to testbucket/testobject

5. 了解更多