Comparing version 0.0.4 to 0.0.5
63
index.js
var sha256 = require('crypto-js/sha256'); | ||
var hmacSha256 = require('crypto-js/hmac-sha256'); | ||
var accessKey = null | ||
var secretKey = null | ||
// | ||
// Any S3 compatible service provider can be used. The default is AWS. | ||
// | ||
// AWS amazonaws.com | ||
// Digital Ocean digitaloceanspaces.com | ||
// Scaleway scw.cloud | ||
// | ||
var serviceProvider = 'amazonaws.com' | ||
/* | ||
@@ -29,3 +17,3 @@ Documentation: | ||
function signAndSendRequest(region, method, bucket, path, body) { | ||
S3.prototype.signAndSendRequest = function(method, bucket, path, body) { | ||
@@ -36,3 +24,3 @@ const amzdate = new Date().toISOString().replace(/[:-]|\.\d{3}/g, '') | ||
const service = 's3'; | ||
const host = `${bucket}.${service}.${region}.${serviceProvider}`; | ||
const host = `${bucket}.${service}.${this.region}.${this.domain}`; | ||
@@ -50,9 +38,9 @@ const endpoint = `https://${host}${path}`; | ||
const credentialScope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'; | ||
const credentialScope = datestamp + '/' + this.region + '/' + service + '/' + 'aws4_request'; | ||
const stringToSign = algorithm + '\n' + amzdate + '\n' + credentialScope + '\n' + sha256(canonicalRequest); | ||
const signingKey = getSignatureKey(secretKey, datestamp, region, service); | ||
const signingKey = getSignatureKey(this.secretKey, datestamp, this.region, service); | ||
const signature = hmacSha256(stringToSign, signingKey); | ||
const authorizationHeader = algorithm + ' ' + 'Credential=' + accessKey + '/' + credentialScope + ',' + 'SignedHeaders=' + signedHeaders + ',' + 'Signature=' + signature; | ||
const authorizationHeader = algorithm + ' ' + 'Credential=' + this.accessKey + '/' + credentialScope + ',' + 'SignedHeaders=' + signedHeaders + ',' + 'Signature=' + signature; | ||
@@ -75,17 +63,32 @@ const params = { | ||
exports.init = (config) => { | ||
accessKey = config.accessKey; | ||
secretKey = config.secretKey; | ||
if (config.serviceProvider !== undefined) { | ||
serviceProvider = config.serviceProvider; | ||
} | ||
}; | ||
// | ||
// Any S3 compatible service provider can be used. The default is AWS. | ||
// | ||
// AWS amazonaws.com | ||
// Digital Ocean digitaloceanspaces.com | ||
// Scaleway scw.cloud | ||
// | ||
var defaultDomain = 'amazonaws.com' | ||
exports.getObject = (region, bucket, filename) => | ||
signAndSendRequest(region, 'GET', bucket, filename); | ||
exports.putObject = (region, bucket, filename, data) => | ||
signAndSendRequest(region, 'PUT', bucket, filename, data); | ||
function S3(config) { | ||
exports.deleteObject = (region, bucket, filename) => | ||
signAndSendRequest(region, 'DELETE', bucket, filename); | ||
this.accessKey = config.accessKey; | ||
this.secretKey = config.secretKey; | ||
this.region = config.region; | ||
this.domain = (config.domain !== undefined) ? config.domain : defaultDomain; | ||
} | ||
S3.prototype.getObject = function(params) { | ||
return this.signAndSendRequest('GET', params.bucket, params.key); | ||
} | ||
S3.prototype.putObject = function(params) { | ||
return this.signAndSendRequest('PUT', params.bucket, params.key, params.body); | ||
} | ||
S3.prototype.deleteObject = function(params) { | ||
return this.signAndSendRequest('DELETE', params.bucket, params.key); | ||
} | ||
module.exports = S3; |
{ | ||
"name": "amazon-s3", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Read and write to AWS S3 using fetch()", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,8 +11,9 @@ # amazon-s3 | ||
```javascript | ||
var s3 = require('amazon-s3') | ||
var S3 = require('amazon-s3') | ||
global.fetch = require('node-fetch') | ||
s3.init({ | ||
var s3 = new S3({ | ||
accessKey: '<private accessKey>', | ||
secretKey: '<private secretKey>' | ||
secretKey: '<private secretKey>', | ||
region: 'us-east-1' | ||
}); | ||
@@ -23,8 +24,7 @@ | ||
let region = 'us-east-1'; | ||
let bucket = 'testbucket'; | ||
let filepath = '/a/test/file.txt'; | ||
let filedata = 'test file contents'; | ||
let key = '/a/test/file.txt'; | ||
let body = 'test file contents'; | ||
let putResponse = await s3.putObject(region, bucket, filepath, filedata) | ||
let putResponse = await s3.putObject({bucket, key, body}) | ||
@@ -34,3 +34,3 @@ console.log(`put status: ${putResponse.status}`) | ||
let getResponse = await s3.getObject(region, bucket, filepath); | ||
let getResponse = await s3.getObject({bucket, key}); | ||
@@ -40,3 +40,3 @@ console.log(`get status: ${getResponse.status}`) | ||
let delResponse = await s3.deleteObject(region, bucket, filepath); | ||
let delResponse = await s3.deleteObject({bucket, key}); | ||
@@ -43,0 +43,0 @@ console.log(`del status: ${delResponse.status}`) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68