Add support for custom S3 endpoint in AWS S3 node

This commit is contained in:
Denis Palashevskii
2020-08-05 14:40:45 +04:00
parent 823ae846bf
commit 4b22df31ad
2 changed files with 124 additions and 44 deletions

View File

@@ -25,16 +25,47 @@ import {
IDataObject,
} from 'n8n-workflow';
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');
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;
const endpointType = this.getNodeParameter('endpoint', 0);
try {
if (endpointType === 'aws') {
credentials = this.getCredentials('aws');
} else {
credentials = this.getCredentials('customS3Endpoint');
}
} catch (error) {
throw new Error(error);
}
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = `${service}.${region || credentials.region}.amazonaws.com`;
let endpoint = endpointType === 'aws' ? `${bucket}.s3.${region || credentials.region}.amazonaws.com` : credentials.endpoint;
// Using path-style for non-AWS services
if (bucket && endpointType === 'customS3Endpoint') {
if (credentials.forcePathStyle) {
path = `/${bucket}${path}`;
} else {
endpoint = `${bucket}.${endpoint}`;
}
}
// Sign AWS API request with the user credentials
const signOpts = {headers: headers || {}, host: endpoint, method, path: `${path}?${queryToString(query).replace(/\+/g, '%2B')}`, body};
const signOpts = {
headers: headers || {},
region: region || credentials.region,
host: endpoint,
method,
path: `${path}?${queryToString(query).replace(/\+/g, '%2B')}`,
service: 's3',
body
};
sign(signOpts, { accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`});
@@ -42,7 +73,7 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
headers: signOpts.headers,
method,
qs: query,
uri: `https://${endpoint}${signOpts.path}`,
uri: `https://${endpoint}${path}`,
body: signOpts.body,
};
@@ -52,7 +83,7 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
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.') {
@@ -66,8 +97,8 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
}
}
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);
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);
try {
return JSON.parse(response);
} catch (e) {
@@ -75,8 +106,8 @@ export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions
}
}
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);
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);
try {
return await new Promise((resolve, reject) => {
parseString(response, { explicitArray: false }, (err, data) => {
@@ -91,14 +122,14 @@ export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions
}
}
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
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
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await awsApiRequestSOAP.call(this, service, method, path, body, query, headers, option, region);
responseData = await s3ApiRequestSOAP.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`)) {