2
0
mirror of https://github.com/therootcompany/s3.js synced 2025-04-21 15:00:42 +00:00
s3.js/bin/s3-download.js
AJ ONeal efdfabb9a4 v1.2.0: bugfix for buckets with invalid domain names, and pass request options
- Buckets with names like 'example.bucket' and 'example_bucket' are valid as paths, but not as domain names.
- allow passthrough support for latest @root/request, which supports pipes and streams
2021-01-26 16:57:20 -07:00

57 lines
1.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
'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;
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('');
}
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);
});