Skip to content

Instantly share code, notes, and snippets.

@meteormatt
Created October 14, 2015 11:00
Show Gist options
  • Save meteormatt/8908e15f6a9fcd1fb8b6 to your computer and use it in GitHub Desktop.
Save meteormatt/8908e15f6a9fcd1fb8b6 to your computer and use it in GitHub Desktop.
multipart.js
var fs = require('fs');
var ALY = require('aliyun-sdk');
var async = require('async');
var BufferHelper = require('bufferhelper');
var oss = new ALY.OSS({
accessKeyId: "你的accessKeyId",
secretAccessKey: "你的secretAccessKey",
endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
apiVersion: '2013-10-15'
});
var fileName = 'test.dmg';
var filePath = '/Users/Meteor/备份/firefox-40.0a2.zh-CN.mac.dmg';
var fileKey = fileName;
// Upload options
var bucket = '你的bucket';
// Upload
var startTime = new Date();
var partNum = 0;
var partSize = 1024 * 1024 * 50; // Minimum 50MB per chunk (except the last part));
var filesize = fs.statSync(filePath).size;
var numPartsLeft = Math.ceil(filesize / partSize);
var maxUploadTries = 3;
var multipartMap = {
Parts: []
};
function completeMultipartUpload(doneParams) {
oss.completeMultipartUpload(doneParams, function (err, data) {
if (err) {
console.log("An error occurred while completing the multipart upload");
console.log(err);
} else {
var delta = (new Date() - startTime) / 1000;
console.log('Completed upload in', delta, 'seconds');
console.log('Final upload data:', data);
}
});
}
function uploadPart(multipart, partParams, tryNum, callback) {
var tryNum = tryNum || 1;
oss.uploadPart(partParams, function (multiErr, mData) {
if (multiErr) {
console.log('multiErr, upload part error:', multiErr);
if (tryNum < maxUploadTries) {
console.log('Retrying upload of part: #', partParams.PartNumber);
uploadPart(multipart, partParams, tryNum + 1, callback);
} else {
console.log('Failed uploading part: #', partParams.PartNumber)
}
return;
}
multipartMap.Parts[this.request.params.PartNumber - 1] = {
ETag: mData.ETag,
PartNumber: Number(this.request.params.PartNumber)
};
console.log("Completed part", this.request.params.PartNumber);
console.log('mData', mData);
callback();
// complete only when all parts uploaded
});
}
// Multipart
console.log("Creating multipart upload for:", fileKey);
oss.createMultipartUpload({
Bucket: bucket,
Key: fileKey
}, function (mpErr, multipart) {
if (mpErr) {
console.log('Error!', mpErr);
return;
}
console.log("Got upload ID", multipart.UploadId);
var rangeStart = 0;
async.whilst(
function () {
return rangeStart < filesize;
},
function (callback) {
var bufferHelper = new BufferHelper();
partNum++;
var end = Math.min(rangeStart + partSize, filesize);
bufferHelper.load(fs.createReadStream(filePath, {start: rangeStart, end: end}), function (err, buffer) {
var partParams = {
Body: buffer,
Bucket: bucket,
Key: fileKey,
PartNumber: String(partNum),
UploadId: multipart.UploadId
};
// Send a single part
console.log('Uploading part: #', partParams.PartNumber, ', Range start:', rangeStart);
rangeStart += partSize;
uploadPart(multipart, partParams, 1, callback);
});
},
function (error) {
if (!error) {
var doneParams = {
Bucket: bucket,
Key: fileKey,
CompleteMultipartUpload: multipartMap,
UploadId: multipart.UploadId
};
console.log("Completing upload...");
completeMultipartUpload(doneParams);
}
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment