From 1b3cdeaadef354fb9049f2667d0dd5ddad6afb21 Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 17 Aug 2017 14:16:29 +0200 Subject: [PATCH 1/7] Allow for policy to be remotely generated. One can now pass the policy as a third argument to the put method. --- src/RNS3.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RNS3.js b/src/RNS3.js index de17e04f..a9649234 100644 --- a/src/RNS3.js +++ b/src/RNS3.js @@ -30,7 +30,7 @@ const setBodyAsParsedXML = (response) => }) export class RNS3 { - static put(file, options) { + static put(file, options, policy = S3Policy.generate(options)) { options = { ...options, key: (options.keyPrefix || '') + file.name, @@ -40,7 +40,6 @@ export class RNS3 { const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}` const method = "POST" - const policy = S3Policy.generate(options) return Request.create(url, method, policy) .set("file", file) From c93c8589f1d7d3ecc77adea34cce418f1d5dbf47 Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 17 Aug 2017 14:27:13 +0200 Subject: [PATCH 2/7] Fixed failing test. --- src/RNS3.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/RNS3.js b/src/RNS3.js index a9649234..6813bdc7 100644 --- a/src/RNS3.js +++ b/src/RNS3.js @@ -30,14 +30,12 @@ const setBodyAsParsedXML = (response) => }) export class RNS3 { - static put(file, options, policy = S3Policy.generate(options)) { - options = { - ...options, - key: (options.keyPrefix || '') + file.name, - date: new Date, - contentType: file.type - } - + static put(file, options, policy = S3Policy.generate({ + ...options, + key: (options.keyPrefix || '') + file.name, + date: new Date, + contentType: file.type + })) { const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}` const method = "POST" From 1ec11ba5f84601cc4e5ce652b62a3c5774d21c0d Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 17 Aug 2017 17:37:05 +0200 Subject: [PATCH 3/7] Updated version and name. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index aeb73ed8..a51a8e9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "react-native-aws3", - "version": "0.0.8", + "name": "@timbrandin/react-native-aws3", + "version": "0.0.9", "description": "Pure JavaScript react native library for uploading to AWS S3", "author": { "name": "Ben Reinhart" From 756f36e8fc7fa8d41ad6451d123008d5852f4c19 Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 24 Aug 2017 09:57:39 +0200 Subject: [PATCH 4/7] Reducing the size of the client bundle. --- src/S3Policy.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/S3Policy.js b/src/S3Policy.js index 8132885d..15080021 100644 --- a/src/S3Policy.js +++ b/src/S3Policy.js @@ -2,7 +2,7 @@ * S3Policy */ -const CryptoJS = require('crypto-js'); +const HmacSHA256 = require('crypto-js').HmacSHA256; const Buffer = global.Buffer || require('buffer').Buffer; const { dateToString } = require('./DateUtils'); @@ -100,17 +100,17 @@ const getEncodedPolicy = (policy) => { } const getSignature = (base64EncodedPolicy, options) => { - return CryptoJS.HmacSHA256( + return HmacSHA256( base64EncodedPolicy, getSignatureKey(options) ).toString(CryptoJS.enc.Hex); } const getSignatureKey = (options) => { - const kDate = CryptoJS.HmacSHA256(options.yyyymmddDate, "AWS4" + options.secretKey); - const kRegion = CryptoJS.HmacSHA256(options.region, kDate); - const kService = CryptoJS.HmacSHA256(AWS_SERVICE_NAME, kRegion); - const kSigning = CryptoJS.HmacSHA256(AWS_REQUEST_POLICY_VERSION, kService); + const kDate = HmacSHA256(options.yyyymmddDate, "AWS4" + options.secretKey); + const kRegion = HmacSHA256(options.region, kDate); + const kService = HmacSHA256(AWS_SERVICE_NAME, kRegion); + const kSigning = HmacSHA256(AWS_REQUEST_POLICY_VERSION, kService); return kSigning; } From 591d53a31979fe9754b80a3c197d488994dbfbcc Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 24 Aug 2017 10:00:00 +0200 Subject: [PATCH 5/7] Added missing require. --- src/S3Policy.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/S3Policy.js b/src/S3Policy.js index 15080021..8d4a0732 100644 --- a/src/S3Policy.js +++ b/src/S3Policy.js @@ -3,6 +3,7 @@ */ const HmacSHA256 = require('crypto-js').HmacSHA256; +const Hex = require('crypto-js').enc.Hex; const Buffer = global.Buffer || require('buffer').Buffer; const { dateToString } = require('./DateUtils'); @@ -103,7 +104,7 @@ const getSignature = (base64EncodedPolicy, options) => { return HmacSHA256( base64EncodedPolicy, getSignatureKey(options) - ).toString(CryptoJS.enc.Hex); + ).toString(Hex); } const getSignatureKey = (options) => { From e04d95349c82bbd5467191317adb726096930e24 Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 24 Aug 2017 10:36:28 +0200 Subject: [PATCH 6/7] Moved out the policy and added a new version without the import of S3Policy which is taking a lot of bytes on the client bundle. --- src/RNS3.js | 38 ++++++++++---------------------------- src/upload.js | 23 +++++++++++++++++++++++ src/utils.js | 21 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 src/upload.js create mode 100644 src/utils.js diff --git a/src/RNS3.js b/src/RNS3.js index 6813bdc7..cda51812 100644 --- a/src/RNS3.js +++ b/src/RNS3.js @@ -3,39 +3,21 @@ */ import { Request } from './Request' +import { setBodyAsParsedXML } from './utils'; import { S3Policy } from './S3Policy' const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com' -const EXPECTED_RESPONSE_KEY_VALUE_RE = { - key: /(.*)<\/Key>/, - etag: /"?([^"]*)"?<\/ETag>/, - bucket: /(.*)<\/Bucket>/, - location: /(.*)<\/Location>/, -} - -const entries = o => - Object.keys(o).map(k => [k, o[k]]) - -const extractResponseValues = (responseText) => - entries(EXPECTED_RESPONSE_KEY_VALUE_RE).reduce((result, [key, regex]) => { - const match = responseText.match(regex) - return { ...result, [key]: match && match[1] } - }, {}) - -const setBodyAsParsedXML = (response) => - ({ - ...response, - body: { postResponse: response.text == null ? null : extractResponseValues(response.text) } - }) - export class RNS3 { - static put(file, options, policy = S3Policy.generate({ - ...options, - key: (options.keyPrefix || '') + file.name, - date: new Date, - contentType: file.type - })) { + static put(file, options, policy) { + if (!policy) { + policy = S3Policy.generate({ + ...options, + key: (options.keyPrefix || '') + file.name, + date: new Date, + contentType: file.type + }) + } const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}` const method = "POST" diff --git a/src/upload.js b/src/upload.js new file mode 100644 index 00000000..3a586a3c --- /dev/null +++ b/src/upload.js @@ -0,0 +1,23 @@ +/** + * RNS3 + */ + +import { Request } from './Request' +import { setBodyAsParsedXML } from './utils'; + +const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com' + +export class RNS3 { + static put(file, options, policy) { + if (!policy) { + throw new Error('missing policy'); + } + const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}` + const method = "POST" + + return Request.create(url, method, policy) + .set("file", file) + .send() + .then(setBodyAsParsedXML) + } +} diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 00000000..322aa4aa --- /dev/null +++ b/src/utils.js @@ -0,0 +1,21 @@ +const EXPECTED_RESPONSE_KEY_VALUE_RE = { + key: /(.*)<\/Key>/, + etag: /"?([^"]*)"?<\/ETag>/, + bucket: /(.*)<\/Bucket>/, + location: /(.*)<\/Location>/, +} + +const entries = o => + Object.keys(o).map(k => [k, o[k]]) + +const extractResponseValues = (responseText) => + entries(EXPECTED_RESPONSE_KEY_VALUE_RE).reduce((result, [key, regex]) => { + const match = responseText.match(regex) + return { ...result, [key]: match && match[1] } + }, {}) + +export const setBodyAsParsedXML = (response) => + ({ + ...response, + body: { postResponse: response.text == null ? null : extractResponseValues(response.text) } + }) From acb0fa8a74c78e816e832badcb03660e24149683 Mon Sep 17 00:00:00 2001 From: Tim Brandin Date: Thu, 24 Aug 2017 10:41:08 +0200 Subject: [PATCH 7/7] Changed upload to be called RNS3Client instead. --- src/{upload.js => RNS3Client.js} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{upload.js => RNS3Client.js} (94%) diff --git a/src/upload.js b/src/RNS3Client.js similarity index 94% rename from src/upload.js rename to src/RNS3Client.js index 3a586a3c..00048276 100644 --- a/src/upload.js +++ b/src/RNS3Client.js @@ -7,7 +7,7 @@ import { setBodyAsParsedXML } from './utils'; const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com' -export class RNS3 { +export class RNS3Client { static put(file, options, policy) { if (!policy) { throw new Error('missing policy');