From ad4a4c1e594bc5f7bda240ad301bd62efeacc5eb Mon Sep 17 00:00:00 2001 From: ricardo Date: Mon, 20 Apr 2020 12:14:13 -0500 Subject: [PATCH 1/2] :zap: Use xml2js to create xml --- .../nodes-base/nodes/Aws/S3/AwsS3.node.ts | 33 ++++++++++++------- packages/nodes-base/package.json | 1 - 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts b/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts index 1cd56dfe2..8a4d5fd0a 100644 --- a/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts +++ b/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts @@ -41,7 +41,9 @@ import { createHash, } from 'crypto'; -import * as js2xmlparser from 'js2xmlparser'; +import { + Builder, + } from 'xml2js'; export class AwsS3 implements INodeType { description: INodeTypeDescription = { @@ -136,13 +138,16 @@ export class AwsS3 implements INodeType { } const body: IDataObject = { - '@': { - xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/', - }, - LocationConstraint: [credentials!.region], + CreateBucketConfiguration: { + '$': { + xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/', + }, + LocationConstraint: [credentials!.region], + } }; - const data = js2xmlparser.parse('CreateBucketConfiguration', body); + const builder = new Builder(); + const data = builder.buildObject(body); responseData = await awsApiRequestSOAP.call(this, `${name}.s3`, 'PUT', '', data, qs, headers); @@ -243,17 +248,23 @@ export class AwsS3 implements INodeType { } else { // delete everything inside the folder const body: IDataObject = { - '@': { - xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/', + Delete: { + '$': { + xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/', + }, + Object: [], }, - Object: [], }; + for (const childObject of responseData) { - (body.Object as IDataObject[]).push({ + //@ts-ignore + (body.Delete.Object as IDataObject[]).push({ Key: childObject.Key as string }); } - const data = js2xmlparser.parse('Delete', body); + + const builder = new Builder(); + const data = builder.buildObject(body); headers['Content-MD5'] = createHash('md5').update(data).digest('base64'); diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3727fbf0f..a63a9e419 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -284,7 +284,6 @@ "gm": "^1.23.1", "googleapis": "~46.0.0", "imap-simple": "^4.3.0", - "js2xmlparser": "^4.0.1", "lodash.get": "^4.4.2", "lodash.set": "^4.3.2", "lodash.unset": "^4.5.2", From fa8c7787a0cdd701f93a4256c9420951a7fb15e0 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Mon, 20 Apr 2020 20:44:22 +0200 Subject: [PATCH 2/2] :zap: Fix an issue with S3-Node --- .../nodes-base/nodes/Aws/GenericFunctions.ts | 2 +- .../nodes-base/nodes/Aws/S3/AwsS3.node.ts | 55 +++++++++++-------- .../nodes/Aws/S3/FileDescription.ts | 2 +- .../nodes/Aws/S3/GenericFunctions.ts | 14 ++--- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/packages/nodes-base/nodes/Aws/GenericFunctions.ts b/packages/nodes-base/nodes/Aws/GenericFunctions.ts index b90efb85f..6ffa92c8e 100644 --- a/packages/nodes-base/nodes/Aws/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Aws/GenericFunctions.ts @@ -28,7 +28,7 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I uri: `https://${endpoint}${signOpts.path}`, body: signOpts.body, }; - console.log('aja') + try { return await this.helpers.request!(options); } catch (error) { diff --git a/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts b/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts index 8a4d5fd0a..ada1835b3 100644 --- a/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts +++ b/packages/nodes-base/nodes/Aws/S3/AwsS3.node.ts @@ -1,7 +1,21 @@ + +import { + snakeCase, + paramCase, +} from 'change-case'; + +import { + createHash, +} from 'crypto'; + +import { + Builder, +} from 'xml2js'; + import { - IExecuteFunctions, BINARY_ENCODING, - } from 'n8n-core'; + IExecuteFunctions, +} from 'n8n-core'; import { IBinaryKeyData, @@ -17,13 +31,13 @@ import { } from './BucketDescription'; import { - folderOperations, - folderFields, + folderFields, + folderOperations, } from './FolderDescription'; import { - fileOperations, fileFields, + fileOperations, } from './FileDescription'; import { @@ -32,19 +46,6 @@ import { awsApiRequestSOAPAllItems, } from './GenericFunctions'; -import { - snakeCase, - paramCase, -} from 'change-case'; - -import { - createHash, - } from 'crypto'; - -import { - Builder, - } from 'xml2js'; - export class AwsS3 implements INodeType { description: INodeTypeDescription = { displayName: 'AWS S3', @@ -76,16 +77,16 @@ export class AwsS3 implements INodeType { name: 'Bucket', value: 'bucket', }, - { - name: 'Folder', - value: 'folder', - }, { name: 'File', value: 'file', }, + { + name: 'Folder', + value: 'folder', + }, ], - default: 'bucket', + default: 'file', description: 'The operation to perform.', }, // BUCKET @@ -142,10 +143,16 @@ export class AwsS3 implements INodeType { '$': { xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/', }, - LocationConstraint: [credentials!.region], } }; + // For some reasons does AWS not allow to supply "us-east-1" if you want to + // create it there it has to be empty?!?! + if (credentials!.region !== 'us-east-1') { + // @ts-ignore + body.CreateBucketConfiguration.LocationConstraint = [credentials!.region]; + } + const builder = new Builder(); const data = builder.buildObject(body); diff --git a/packages/nodes-base/nodes/Aws/S3/FileDescription.ts b/packages/nodes-base/nodes/Aws/S3/FileDescription.ts index fabebdd96..b1c6d133a 100644 --- a/packages/nodes-base/nodes/Aws/S3/FileDescription.ts +++ b/packages/nodes-base/nodes/Aws/S3/FileDescription.ts @@ -41,7 +41,7 @@ export const fileOperations = [ description: 'Upload a file', }, ], - default: 'copy', + default: 'download', description: 'The operation to perform.', }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts b/packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts index 3c292edc4..e27ce52ba 100644 --- a/packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts @@ -1,14 +1,18 @@ import { sign, - } from 'aws4'; +} from 'aws4'; + +import { + get, +} from 'lodash'; import { OptionsWithUri, - } from 'request'; +} from 'request'; import { parseString, - } from 'xml2js'; +} from 'xml2js'; import { IExecuteFunctions, @@ -21,10 +25,6 @@ import { IDataObject, } from 'n8n-workflow'; -import { - get, -} from 'lodash'; - export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}): Promise { // tslint:disable-line:no-any const credentials = this.getCredentials('aws'); if (credentials === undefined) {