下载文件

MSS js SDK提供了丰富的文件下载接口,用户可以通过以下方式下载文件:

  • 下载文件到本地文件
  • 下载到文件内存
  • 流式下载
  • HTTP下载

下载文件到本地文件

把Object的内容下载到指定的本地文件中。

var MSS = require('mos-mss');
var path = require('path');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>',
    bucket: 'Bucket'
});

var result = client.getObject('test.json', path.join(__dirname, './data/test.json'));

result.then(function (data) {
    console.log(data);
});

下载文件到内存

把Object的内容下载到内存。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>',
    bucket: 'Bucket'
});

var result = client.getBuffer('test');

result.then(function (res) {
    console.log(res);
});

流式下载

getStream返回数据中stream 为 Readable Stream,用户可进行文件流处理。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>',
    bucket: 'Bucket'
});

var writeStream = fs.createWriteStream(path.join(__dirname, './data/test.json'));

var result = client.getStream('ObjectKey');

result.then(function (data) {
    data.stream.pipe(writeStream);
    data.stream.on('end', function () {
        console.log('success');
    });
    data.stream.on('error', function (err) {
        console.log('fail', err);
    });
});

HTTP下载

使用signatureUrl方法生成可下载的HTTP地址。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>',
    bucket: 'Bucket'
});

var result = client.signatureUrl('ObjectKey');

result.then(function (res) {
    console.log(res);
});