A Node.js module for streaming data to Aliyun oss via the multipart upload API
用Aliyun oss 的 Multipart upload API 实现的Node.js模块,通过stream的方式上传文件。
官方指定Nodejs模块~
var ALY = require('aliyun-sdk'),
fs = require('fs');
var ossStream = require('aliyun-oss-upload-stream')(new ALY.OSS({
accessKeyId: '在阿里云OSS申请的 accessKeyId',
secretAccessKey: '在阿里云OSS申请的 secretAccessKey',
endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
apiVersion: '2013-10-15'
}));
var upload = ossStream.upload({
Bucket: 'Bucket',
Key: 'Key (可以理解为文件名)'
});
// 可选配置
upload.minPartSize(1048576); // 1M,表示每块part大小至少大于1M
upload.on('error', function (error) {
console.log('error:', error);
});
upload.on('part', function (part) {
console.log('part:', part);
});
upload.on('uploaded', function (details) {
var s = (new Date() - startTime) / 1000;
console.log('details:', details);
console.log('Completed upload in %d seconds', s);
});
var read = fs.createReadStream('./photo.jpg');
read.pipe(upload);
var startTime = new Date();
var ALY = require('aliyun-sdk');
var ossStream = require('aliyun-oss-upload-stream')(new ALY.OSS({
accessKeyId: '在阿里云OSS申请的 accessKeyId',
secretAccessKey: '在阿里云OSS申请的 secretAccessKey',
endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
apiVersion: '2013-10-15'
}));
var upload = ossStream.upload({
Bucket: 'Bucket-Name',
Key: 'Key-Name'
});
var read = fs.createReadStream('./photo.jpg');
read.pipe(upload);
upload.minPartSize
用于调整每次上传一小块数据的大小,不得低于200KB,默认为200KB,如果经常上传大文件,建议用此方法把值调整大一些
var upload = ossStream.upload({
Bucket: 'Bucket-Name',
Key: 'Key-Name'
});
// 可选配置
upload.minPartSize(1048576); // 1M,表示每块part大小至少大于1M
var read = fs.createReadStream('./photo.jpg');
read.pipe(upload);
error
当上传过程中发生错误,触发error事件,回调函数参数为错误信息
upload.on('error', function (error) {
console.log('error:', error);
});
part
上传文件的过程中,会触发part事件,回调函数参数为当前分片的信息
upload.on('part', function (part) {
console.log('part:', part);
});
uploaded
上传成功后触发该事件,回调函数参数为完整的 Object
upload.on('uploaded', function (details) {
console.log('details:', details);
});
npm i --save aliyun-oss-upload-stream
PS: 如果大家使用过程中,发现什么问题或者需要添加什么功能,及时通知我哈~ 我会及时更新和发布新版本~
Copyright (c) 2015 Berwin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.