61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
#! /usr/bin/env runjs
|
|
|
|
var oss = require('component-oss.js');
|
|
var args = require('component-args.js');
|
|
var counter = require('component-counter.js');
|
|
|
|
oss.requireJARs();
|
|
|
|
var main = () => {
|
|
var start = $$.date().millis();
|
|
var parsedArgs = args.parseARGS();
|
|
|
|
if ((parsedArgs.args.length < 1) || (!(parsedArgs.values['config']))) {
|
|
println('Config file or file is not assigned.');
|
|
println('ossuploader.js --config config.json filename.ext [target_filename.ext]');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var config = $$.rFile(parsedArgs.values['config']).string();
|
|
configJSON = JSON.parse(config);
|
|
} catch (e) {
|
|
println('Parse config error: ' + e);
|
|
}
|
|
|
|
if (!configJSON || !configJSON.bucket || !configJSON.endpoint || !configJSON.accessKey || !configJSON.secretKey) {
|
|
println('----- Config format -----');
|
|
println('{');
|
|
println(' \"endpoint\": \"<ENDPOINT>\",');
|
|
println(' \"bucket\": \"<BUCKET>\",');
|
|
println(' \"accessKey\": \"<AK>\",');
|
|
println(' \"secretKey\": \"<SK>\",');
|
|
println('}');
|
|
return;
|
|
}
|
|
|
|
println('OSS info: ' + configJSON.bucket + '@' + configJSON.endpoint);
|
|
|
|
oss.newClient(configJSON.endpoint, configJSON.accessKey, configJSON.secretKey).runWith((client) => {
|
|
if (!client.doesBucketExist(configJSON.bucket)) {
|
|
println("Bucket NOT exists: " + configJSON.bucket);
|
|
return;
|
|
}
|
|
|
|
var localFile = parsedArgs.args[0];
|
|
var remoteFile = parsedArgs.args[1] || parsedArgs.args[0].replace(/[^a-zA-Z0-9\.\-]/, '_');
|
|
|
|
println('Uploading file: ' + localFile + ' -> ' + remoteFile);
|
|
|
|
var counterIS = oss.createCounterIS(localFile);
|
|
client.putObject(configJSON.bucket, remoteFile, counterIS);
|
|
println();
|
|
counterIS.close();
|
|
|
|
println('Upload file finished, cost: ' + ($$.date().millis() - start) + ' ms.');
|
|
});
|
|
};
|
|
|
|
main();
|
|
|