s3.js/bin/s3-download.js

55 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2020-03-17 02:38:50 +00:00
#!/usr/bin/env node
2020-03-14 03:40:38 +00:00
'use strict';
require('dotenv').config();
var env = process.env;
var s3 = require('../index.js');
var accessKeyId = env.AWS_ACCESS_KEY || env.AWS_ACCESS_KEY_ID;
2020-03-14 03:40:38 +00:00
var secretAccessKey = env.AWS_SECRET_ACCESS_KEY;
var region = env.AWS_REGION;
var bucket = env.AWS_BUCKET;
var prefix = env.AWS_BUCKET_PREFIX;
var key = process.argv[2];
var filepath = process.argv[3];
var fs = require('fs');
if (!key || !filepath) {
console.info('Usage: s3-download.js s3-key-name ./path/to/file.bin');
process.exit(1);
}
async function run() {
// GET STREAMED FILE
await s3
.get({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
.then(function(resp) {
console.log(resp.url);
return fs.promises.writeFile(filepath, resp.body);
})
.catch(function(err) {
console.error('Error:');
if (err.response) {
console.error(err.url);
console.error('GET Response:');
2020-03-14 03:40:38 +00:00
console.error(err.response.statusCode);
console.error(err.response.headers);
console.error(err.response.body.toString('utf8'));
2020-03-14 03:40:38 +00:00
} else {
console.error(err);
}
process.exit(1);
});
}
run();