ali-oss
Version:
aliyun oss(object storage service) node client
61 lines (52 loc) • 1.33 kB
JavaScript
const fs = require('fs');
const is = require('is-type-of');
const proto = exports;
/**
* get
* @param {String} name - object name
* @param {String | Stream} file
* @param {Object} options
* @param {{res}}
*/
proto.get = async function get(name, file, options = {}) {
let writeStream = null;
let needDestroy = false;
if (is.writableStream(file)) {
writeStream = file;
} else if (is.string(file)) {
writeStream = fs.createWriteStream(file);
needDestroy = true;
} else {
// get(name, options)
options = file;
}
options = options || {};
options.subres = Object.assign({}, options.subres);
if (options.versionId) {
options.subres.versionId = options.versionId;
}
if (options.process) {
options.subres['x-oss-process'] = options.process;
}
let result;
try {
const params = this._objectRequestParams('GET', name, options);
params.writeStream = writeStream;
params.successStatuses = [200, 206, 304];
result = await this.request(params);
if (needDestroy) {
writeStream.destroy();
}
} catch (err) {
if (needDestroy) {
writeStream.destroy();
// should delete the exists file before throw error
await this._deleteFileSafe(file);
}
throw err;
}
return {
res: result.res,
content: result.data
};
};