Revert AWS S3 node to its original state

This commit is contained in:
Denis Palashevskii 2020-08-18 17:11:20 +04:00
parent 77eb0f4955
commit 0cf3b069a9
2 changed files with 44 additions and 73 deletions

View file

@ -41,9 +41,9 @@ import {
} from './FileDescription';
import {
s3ApiRequestREST,
s3ApiRequestSOAP,
s3ApiRequestSOAPAllItems,
awsApiRequestREST,
awsApiRequestSOAP,
awsApiRequestSOAPAllItems,
} from './GenericFunctions';
export class AwsS3 implements INodeType {
@ -65,7 +65,7 @@ export class AwsS3 implements INodeType {
{
name: 'aws',
required: true,
},
}
],
properties: [
{
@ -113,15 +113,7 @@ export class AwsS3 implements INodeType {
if (resource === 'bucket') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
if (operation === 'create') {
let credentials;
try {
credentials = this.getCredentials('aws');
} catch (error) {
throw new Error(error);
}
const credentials = this.getCredentials('aws');
const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.acl) {
@ -166,7 +158,7 @@ export class AwsS3 implements INodeType {
const builder = new Builder();
data = builder.buildObject(body);
}
responseData = await s3ApiRequestSOAP.call(this, `${name}`, 'PUT', '', data, qs, headers);
responseData = await awsApiRequestSOAP.call(this, `${name}.s3`, 'PUT', '', data, qs, headers);
returnData.push({ success: true });
}
@ -174,10 +166,10 @@ export class AwsS3 implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
if (returnAll) {
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', '', 'GET', '');
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', 's3', 'GET', '');
} else {
qs.limit = this.getNodeParameter('limit', 0) as number;
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', '', 'GET', '', '', qs);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', 's3', 'GET', '', '', qs);
responseData = responseData.slice(0, qs.limit);
}
returnData.push.apply(returnData, responseData);
@ -216,15 +208,15 @@ export class AwsS3 implements INodeType {
qs['list-type'] = 2;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._ as string;
if (returnAll) {
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
} else {
qs['max-keys'] = this.getNodeParameter('limit', 0) as number;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
responseData = responseData.ListBucketResult.Contents;
}
if (Array.isArray(responseData)) {
@ -251,11 +243,11 @@ export class AwsS3 implements INodeType {
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = (snakeCase(additionalFields.storageClass as string)).toUpperCase();
}
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', path, '', qs, headers, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'PUT', path, '', qs, headers, {}, region);
returnData.push({ success: true });
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html
@ -263,16 +255,16 @@ export class AwsS3 implements INodeType {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const folderKey = this.getNodeParameter('folderKey', i) as string;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '/', '', { 'list-type': 2, prefix: folderKey }, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '/', '', { 'list-type': 2, prefix: folderKey }, {}, {}, region);
// folder empty then just delete it
if (responseData.length === 0) {
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'DELETE', `/${folderKey}`, '', qs, {}, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'DELETE', `/${folderKey}`, '', qs, {}, {}, region);
responseData = { deleted: [ { 'Key': folderKey } ] };
@ -301,7 +293,7 @@ export class AwsS3 implements INodeType {
headers['Content-Type'] = 'application/xml';
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'POST', '/', data, { delete: '' } , headers, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'POST', '/', data, { delete: '' } , headers, {}, region);
responseData = { deleted: responseData.DeleteResult.Deleted };
}
@ -323,15 +315,15 @@ export class AwsS3 implements INodeType {
qs['list-type'] = 2;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
} else {
qs.limit = this.getNodeParameter('limit', 0) as number;
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
}
if (Array.isArray(responseData)) {
responseData = responseData.filter((e: IDataObject) => (e.Key as string).endsWith('/') && e.Size === '0' && e.Key !== options.folderKey);
@ -412,11 +404,11 @@ export class AwsS3 implements INodeType {
const destination = `/${destinationParts.slice(2, destinationParts.length).join('/')}`;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', destination, '', qs, headers, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'PUT', destination, '', qs, headers, {}, region);
returnData.push(responseData.CopyObjectResult);
}
@ -433,11 +425,11 @@ export class AwsS3 implements INodeType {
throw new Error('Downloding a whole directory is not yet supported, please provide a file key');
}
let region = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
let region = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
region = region.LocationConstraint._;
const response = await s3ApiRequestREST.call(this, bucketName, 'GET', `/${fileKey}`, '', qs, {}, { encoding: null, resolveWithFullResponse: true }, region);
const response = await awsApiRequestREST.call(this, `${bucketName}.s3`, 'GET', `/${fileKey}`, '', qs, {}, { encoding: null, resolveWithFullResponse: true }, region);
let mimeType: string | undefined;
if (response.headers['content-type']) {
@ -476,11 +468,11 @@ export class AwsS3 implements INodeType {
qs.versionId = options.versionId as string;
}
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'DELETE', `/${fileKey}`, '', qs, {}, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'DELETE', `/${fileKey}`, '', qs, {}, {}, region);
returnData.push({ success: true });
}
@ -502,15 +494,15 @@ export class AwsS3 implements INodeType {
qs['list-type'] = 2;
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
} else {
qs.limit = this.getNodeParameter('limit', 0) as number;
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
responseData = await awsApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', `${bucketName}.s3`, 'GET', '', '', qs, {}, {}, region);
responseData = responseData.splice(0, qs.limit);
}
if (Array.isArray(responseData)) {
@ -589,7 +581,7 @@ export class AwsS3 implements INodeType {
headers['x-amz-tagging'] = tags.join('&');
}
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', { location: '' });
const region = responseData.LocationConstraint._;
@ -612,7 +604,7 @@ export class AwsS3 implements INodeType {
headers['Content-MD5'] = createHash('md5').update(body).digest('base64');
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', `${path}${fileName || binaryData.fileName}`, body, qs, headers, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'PUT', `${path}${fileName || binaryData.fileName}`, body, qs, headers, {}, region);
} else {
@ -624,7 +616,7 @@ export class AwsS3 implements INodeType {
headers['Content-MD5'] = createHash('md5').update(fileContent).digest('base64');
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', `${path}${fileName}`, body, qs, headers, {}, region);
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'PUT', `${path}${fileName}`, body, qs, headers, {}, region);
}
returnData.push({ success: true });
}

View file

@ -25,37 +25,16 @@ import {
IDataObject,
} from 'n8n-workflow';
import { URL } from 'url';
export async function s3ApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, bucket: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
let credentials;
try {
credentials = this.getCredentials('aws');
} catch (error) {
throw new Error(error);
}
export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = new URL(`https://${bucket}.s3.${region || credentials.region}.amazonaws.com`);
endpoint.pathname = path;
const endpoint = `${service}.${region || credentials.region}.amazonaws.com`;
// Sign AWS API request with the user credentials
const signOpts = {
headers: headers || {},
region: region || credentials.region,
host: endpoint.host,
method,
path: `${path}?${queryToString(query).replace(/\+/g, '%2B')}`,
service: 's3',
body
};
const signOpts = {headers: headers || {}, host: endpoint, method, path: `${path}?${queryToString(query).replace(/\+/g, '%2B')}`, body};
sign(signOpts, { accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`});
@ -63,7 +42,7 @@ export async function s3ApiRequest(this: IHookFunctions | IExecuteFunctions | IL
headers: signOpts.headers,
method,
qs: query,
uri: endpoint,
uri: `https://${endpoint}${signOpts.path}`,
body: signOpts.body,
};
@ -73,7 +52,7 @@ export async function s3ApiRequest(this: IHookFunctions | IExecuteFunctions | IL
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = error.response?.body.message || error.response?.body.Message || error.message;
const errorMessage = error.response.body.message || error.response.body.Message || error.message;
if (error.statusCode === 403) {
if (errorMessage === 'The security token included in the request is invalid.') {
@ -87,8 +66,8 @@ export async function s3ApiRequest(this: IHookFunctions | IExecuteFunctions | IL
}
}
export async function s3ApiRequestREST(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, bucket: string, method: string, path: string, body?: string, query: IDataObject = {}, headers?: object, options: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await s3ApiRequest.call(this, bucket, method, path, body, query, headers, options, region);
export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, query: IDataObject = {}, headers?: object, options: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, query, headers, options, region);
try {
return JSON.parse(response);
} catch (e) {
@ -96,8 +75,8 @@ export async function s3ApiRequestREST(this: IHookFunctions | IExecuteFunctions
}
}
export async function s3ApiRequestSOAP(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, bucket: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await s3ApiRequest.call(this, bucket, method, path, body, query, headers, option, region);
export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, query, headers, option, region);
try {
return await new Promise((resolve, reject) => {
parseString(response, { explicitArray: false }, (err, data) => {
@ -112,14 +91,14 @@ export async function s3ApiRequestSOAP(this: IHookFunctions | IExecuteFunctions
}
}
export async function s3ApiRequestSOAPAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, propertyName: string, service: string, method: string, path: string, body?: string, query: IDataObject = {}, headers: IDataObject = {}, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
export async function awsApiRequestSOAPAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, propertyName: string, service: string, method: string, path: string, body?: string, query: IDataObject = {}, headers: IDataObject = {}, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await s3ApiRequestSOAP.call(this, service, method, path, body, query, headers, option, region);
responseData = await awsApiRequestSOAP.call(this, service, method, path, body, query, headers, option, region);
//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {