mirror of https://github.com/therootcompany/s3.js
use new s3 url style
This commit is contained in:
parent
9a332a12d9
commit
aee8c5f182
|
@ -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();
|
60
index.js
60
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(
|
||||
|
|
Loading…
Reference in New Issue