From aee8c5f18264e4ef86302220ae9b5fd448dac6c7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 13 Mar 2020 21:40:38 -0600 Subject: [PATCH] use new s3 url style --- bin/s3-download.js | 51 +++++++++++++++++++++++++++++++++++++++ index.js | 60 ++++++++++++++++++++++++++++------------------ 2 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 bin/s3-download.js diff --git a/bin/s3-download.js b/bin/s3-download.js new file mode 100644 index 0000000..bba8361 --- /dev/null +++ b/bin/s3-download.js @@ -0,0 +1,51 @@ +'use strict'; + +require('dotenv').config(); +var env = process.env; +var s3 = require('../index.js'); + +var accessKeyId = env.AWS_ACCESS_KEY; +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:'); + console.error('GET Response:'); + if (err.response) { + console.error(err.response.statusCode); + console.error(err.response.headers); + console.error(err.url); + } else { + console.error(err); + } + process.exit(1); + }); +} + +run(); diff --git a/index.js b/index.js index d75f25e..2d2dff5 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var env = process.env; module.exports = { // HEAD head: function({ + host, accessKeyId, secretAccessKey, region, @@ -44,19 +45,20 @@ module.exports = { } var signed = aws4.sign( { - // host: awsHost + host: host || bucket + '.s3.amazonaws.com', service: 's3', region: region, - path: '/' + bucket + '/' + prefix + key, + path: (host ? '/' + bucket : '') + '/' + prefix + key, method: 'HEAD', signQuery: true }, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey } ); - var url = 'https://' + signed.hostname + signed.path; + var url = 'https://' + signed.host + signed.path; return request({ method: 'HEAD', url }).then(function(resp) { if (200 === resp.statusCode) { + resp.url = url; return resp; } var err = new Error( @@ -72,6 +74,7 @@ module.exports = { // GET get: function({ + host, accessKeyId, secretAccessKey, region, @@ -86,40 +89,46 @@ module.exports = { } var signed = aws4.sign( { + host: host || bucket + '.s3.amazonaws.com', service: 's3', region: region, - path: '/' + bucket + '/' + prefix + key, + path: (host ? '/' + bucket : '') + '/' + prefix + key, method: 'GET', signQuery: true }, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey } ); - var url = 'https://' + signed.hostname + signed.path; + var url = 'https://' + signed.host + signed.path; // stay binary by default var encoding = null; if (json) { encoding = undefined; } - return request({ method: 'GET', url, encoding: null, json: json }).then( - function(resp) { - if (200 === resp.statusCode) { - return resp; - } - var err = new Error( - 'expected status 200 but got ' + - resp.statusCode + - '. See err.response for more info.' - ); - err.url = url; - err.response = resp; - throw err; + return request({ + method: 'GET', + url, + encoding: encoding, + json: json + }).then(function(resp) { + if (200 === resp.statusCode) { + resp.url = url; + return resp; } - ); + var err = new Error( + 'expected status 200 but got ' + + resp.statusCode + + '. See err.response for more info.' + ); + err.url = url; + err.response = resp; + throw err; + }); }, // PUT set: function({ + host, accessKeyId, secretAccessKey, region, @@ -135,15 +144,16 @@ module.exports = { } var signed = aws4.sign( { + host: host || bucket + '.s3.amazonaws.com', service: 's3', region: region, - path: '/' + bucket + '/' + prefix + key, + path: (host ? '/' + bucket : '') + '/' + prefix + key, method: 'PUT', signQuery: true }, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey } ); - var url = 'https://' + signed.hostname + signed.path; + var url = 'https://' + signed.host + signed.path; var headers = {}; if ('undefined' !== typeof size) { headers['Content-Length'] = size; @@ -153,6 +163,7 @@ module.exports = { resp ) { if (200 === resp.statusCode) { + resp.url = url; return resp; } var err = new Error( @@ -168,6 +179,7 @@ module.exports = { // DELETE del: function({ + host, accessKeyId, secretAccessKey, region, @@ -181,18 +193,20 @@ module.exports = { } var signed = aws4.sign( { + host: host || bucket + '.s3.amazonaws.com', service: 's3', region: region, - path: '/' + bucket + '/' + prefix + key, + path: (host ? '/' + bucket : '') + '/' + prefix + key, method: 'DELETE', signQuery: true }, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey } ); - var url = 'https://' + signed.hostname + signed.path; + var url = 'https://' + signed.host + signed.path; return request({ method: 'DELETE', url }).then(function(resp) { if (204 === resp.statusCode) { + resp.url = url; return resp; } var err = new Error(