s3.js/bin/s3-download.js

57 lines
1.3 KiB
JavaScript
Raw 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
var resp = await s3.get({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key,
stream: filepath
});
console.log('Downloading', resp.url);
await resp.stream;
console.log('');
console.log('Saved as', filepath);
console.log('');
2020-03-14 03:40:38 +00:00
}
run().catch(function (err) {
console.error('Error:');
if (err.response) {
console.error(err.url);
console.error('GET Response:');
console.error(err.response.statusCode);
console.error(err.response.headers);
console.error(err.response.body.toString('utf8'));
} else {
console.error(err);
}
process.exit(1);
});