feat(AwsS3 Node): Small overhaul of the node with multipart uploading (#6017)

* Create new version for S3

* Update S3 to new aws s3 methods

* Switch from SAOP to Rest api

* Add multipart request

* Seperate stream into chunks and send the multipart

* Fix chunk into buffer

* Fix wrong sha256 mismatch

* Add abort multipart on error

* Complete multipart and list parts

* Change format to xml and add a minmum size of 5MB for each part

* Fix returned data for uploading a file

* Remove console.logs

* Seperate needed headers and multipart headers

* Throw error on aborting, remove console.logs

* Remove soap request from generic function

* Keep buffer

* Add unit test for V2

* fix upload file content body

* removed unused import

* Fix bug where the object was too smal and used only one part

* Fix naming for bucket name

* Fix issue with file name not returning data

* Add parent name

* Remove console.logs

* Add content type

* fix headears for other upload mode

---------

Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
agobrech 2023-06-15 13:19:22 +02:00 committed by GitHub
parent da105f468b
commit 109442f38f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 3682 additions and 911 deletions

View file

@ -287,7 +287,6 @@ export class Aws implements ICredentialType {
let body = requestOptions.body; let body = requestOptions.body;
let region = credentials.region; let region = credentials.region;
let query = requestOptions.qs?.query as IDataObject; let query = requestOptions.qs?.query as IDataObject;
// ! Workaround as we still use the OptionsWithUri interface which uses uri instead of url // ! Workaround as we still use the OptionsWithUri interface which uses uri instead of url
// ! To change when we replace the interface with IHttpRequestOptions // ! To change when we replace the interface with IHttpRequestOptions
const requestWithUri = requestOptions as unknown as OptionsWithUri; const requestWithUri = requestOptions as unknown as OptionsWithUri;

View file

@ -1,908 +1,27 @@
import { paramCase, snakeCase } from 'change-case'; import type { INodeTypeBaseDescription, IVersionedNodeType } from 'n8n-workflow';
import { VersionedNodeType } from 'n8n-workflow';
import { createHash } from 'crypto';
import { AwsS3V1 } from './V1/AwsS3V1.node';
import { Builder } from 'xml2js';
import { AwsS3V2 } from './V2/AwsS3V2.node';
import type {
IDataObject, export class AwsS3 extends VersionedNodeType {
IExecuteFunctions, constructor() {
INodeExecutionData, const baseDescription: INodeTypeBaseDescription = {
INodeType, displayName: 'AwsS3',
INodeTypeDescription, name: 'awsS3',
} from 'n8n-workflow'; icon: 'file:s3.svg',
import { NodeOperationError } from 'n8n-workflow'; group: ['output'],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
import { bucketFields, bucketOperations } from './BucketDescription'; description: 'Sends data to AWS S3',
defaultVersion: 2,
import { folderFields, folderOperations } from './FolderDescription'; };
import { fileFields, fileOperations } from './FileDescription'; const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new AwsS3V1(baseDescription),
import { 2: new AwsS3V2(baseDescription),
awsApiRequestREST, };
awsApiRequestSOAP,
awsApiRequestSOAPAllItems, super(nodeVersions, baseDescription);
} from './GenericFunctions';
export class AwsS3 implements INodeType {
description: INodeTypeDescription = {
displayName: 'AWS S3',
name: 'awsS3',
icon: 'file:s3.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Sends data to AWS S3',
defaults: {
name: 'AWS S3',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Bucket',
value: 'bucket',
},
{
name: 'File',
value: 'file',
},
{
name: 'Folder',
value: 'folder',
},
],
default: 'file',
},
// BUCKET
...bucketOperations,
...bucketFields,
// FOLDER
...folderOperations,
...folderFields,
// UPLOAD
...fileOperations,
...fileFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
const headers: IDataObject = {};
try {
if (resource === 'bucket') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
if (operation === 'create') {
const credentials = await this.getCredentials('aws');
const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.bucketObjectLockEnabled) {
headers['x-amz-bucket-object-lock-enabled'] =
additionalFields.bucketObjectLockEnabled as boolean;
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWrite) {
headers['x-amz-grant-write'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
let region = credentials.region as string;
if (additionalFields.region) {
region = additionalFields.region as string;
}
const body: IDataObject = {
CreateBucketConfiguration: {
$: {
xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/',
},
},
};
let data = '';
// if credentials has the S3 defaul region (us-east-1) the body (XML) does not have to be sent.
if (region !== 'us-east-1') {
// @ts-ignore
body.CreateBucketConfiguration.LocationConstraint = [region];
const builder = new Builder();
data = builder.buildObject(body);
}
responseData = await awsApiRequestSOAP.call(
this,
`${name}.s3`,
'PUT',
'',
data,
qs,
headers,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html
if (operation === 'delete') {
const name = this.getNodeParameter('name', i) as string;
responseData = await awsApiRequestSOAP.call(
this,
`${name}.s3`,
'DELETE',
'',
'',
{},
headers,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', 0);
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListAllMyBucketsResult.Buckets.Bucket',
's3',
'GET',
'',
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListAllMyBucketsResult.Buckets.Bucket',
's3',
'GET',
'',
'',
qs,
);
responseData = responseData.slice(0, qs.limit);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'search') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
if (additionalFields.prefix) {
qs.prefix = additionalFields.prefix as string;
}
if (additionalFields.encodingType) {
qs['encoding-type'] = additionalFields.encodingType as string;
}
if (additionalFields.delimiter) {
qs.delimiter = additionalFields.delimiter as string;
}
if (additionalFields.fetchOwner) {
qs['fetch-owner'] = additionalFields.fetchOwner as string;
}
if (additionalFields.startAfter) {
qs['start-after'] = additionalFields.startAfter as string;
}
if (additionalFields.requesterPays) {
qs['x-amz-request-payer'] = 'requester';
}
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._ as string;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region,
);
} else {
qs['max-keys'] = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region,
);
responseData = responseData.ListBucketResult.Contents;
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
if (resource === 'folder') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
if (operation === 'create') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const folderName = this.getNodeParameter('folderName', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
let path = `/${folderName}/`;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.parentFolderKey) {
path = `/${additionalFields.parentFolderKey}${folderName}/`;
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
path,
'',
qs,
headers,
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html
if (operation === 'delete') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const folderKey = this.getNodeParameter('folderKey', i) as string;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'/',
'',
{ 'list-type': 2, prefix: folderKey },
{},
{},
region as string,
);
// folder empty then just delete it
if (responseData.length === 0) {
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'DELETE',
`/${folderKey}`,
'',
qs,
{},
{},
region as string,
);
responseData = { deleted: [{ Key: folderKey }] };
} else {
// delete everything inside the folder
const body: IDataObject = {
Delete: {
$: {
xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/',
},
Object: [],
},
};
for (const childObject of responseData) {
//@ts-ignore
(body.Delete.Object as IDataObject[]).push({
Key: childObject.Key as string,
});
}
const builder = new Builder();
const data = builder.buildObject(body);
headers['Content-MD5'] = createHash('md5').update(data).digest('base64');
headers['Content-Type'] = 'application/xml';
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'POST',
'/',
data,
{ delete: '' },
headers,
{},
region as string,
);
responseData = { deleted: responseData.DeleteResult.Deleted };
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'getAll') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const options = this.getNodeParameter('options', 0);
if (options.folderKey) {
qs.prefix = options.folderKey as string;
}
if (options.fetchOwner) {
qs['fetch-owner'] = options.fetchOwner as string;
}
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
}
if (Array.isArray(responseData)) {
responseData = responseData.filter(
(e: IDataObject) =>
(e.Key as string).endsWith('/') && e.Size === '0' && e.Key !== options.folderKey,
);
if (qs.limit) {
responseData = responseData.splice(0, qs.limit as number);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
}
if (resource === 'file') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html
if (operation === 'copy') {
const sourcePath = this.getNodeParameter('sourcePath', i) as string;
const destinationPath = this.getNodeParameter('destinationPath', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
headers['x-amz-copy-source'] = sourcePath;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
if (additionalFields.lockLegalHold) {
headers['x-amz-object-lock-legal-hold'] = (additionalFields.lockLegalHold as boolean)
? 'ON'
: 'OFF';
}
if (additionalFields.lockMode) {
headers['x-amz-object-lock-mode'] = (
additionalFields.lockMode as string
).toUpperCase();
}
if (additionalFields.lockRetainUntilDate) {
headers['x-amz-object-lock-retain-until-date'] =
additionalFields.lockRetainUntilDate as string;
}
if (additionalFields.serverSideEncryption) {
headers['x-amz-server-side-encryption'] =
additionalFields.serverSideEncryption as string;
}
if (additionalFields.encryptionAwsKmsKeyId) {
headers['x-amz-server-side-encryption-aws-kms-key-id'] =
additionalFields.encryptionAwsKmsKeyId as string;
}
if (additionalFields.serverSideEncryptionContext) {
headers['x-amz-server-side-encryption-context'] =
additionalFields.serverSideEncryptionContext as string;
}
if (additionalFields.serversideEncryptionCustomerAlgorithm) {
headers['x-amz-server-side-encryption-customer-algorithm'] =
additionalFields.serversideEncryptionCustomerAlgorithm as string;
}
if (additionalFields.serversideEncryptionCustomerKey) {
headers['x-amz-server-side-encryption-customer-key'] =
additionalFields.serversideEncryptionCustomerKey as string;
}
if (additionalFields.serversideEncryptionCustomerKeyMD5) {
headers['x-amz-server-side-encryption-customer-key-MD5'] =
additionalFields.serversideEncryptionCustomerKeyMD5 as string;
}
if (additionalFields.taggingDirective) {
headers['x-amz-tagging-directive'] = (
additionalFields.taggingDirective as string
).toUpperCase();
}
if (additionalFields.metadataDirective) {
headers['x-amz-metadata-directive'] = (
additionalFields.metadataDirective as string
).toUpperCase();
}
const destinationParts = destinationPath.split('/');
const bucketName = destinationParts[1];
const destination = `/${destinationParts.slice(2, destinationParts.length).join('/')}`;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
destination,
'',
qs,
headers,
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData.CopyObjectResult as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html
if (operation === 'download') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileKey = this.getNodeParameter('fileKey', i) as string;
const fileName = fileKey.split('/')[fileKey.split('/').length - 1];
if (fileKey.substring(fileKey.length - 1) === '/') {
throw new NodeOperationError(
this.getNode(),
'Downloading a whole directory is not yet supported, please provide a file key',
);
}
let region = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
region = region.LocationConstraint._;
const response = await awsApiRequestREST.call(
this,
`${bucketName}.s3`,
'GET',
`/${fileKey}`,
'',
qs,
{},
{ encoding: null, resolveWithFullResponse: true },
region as string,
);
let mimeType: string | undefined;
if (response.headers['content-type']) {
mimeType = response.headers['content-type'];
}
const newItem: INodeExecutionData = {
json: items[i].json,
binary: {},
};
if (items[i].binary !== undefined && newItem.binary) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, items[i].binary);
}
items[i] = newItem;
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
const data = Buffer.from(response.body as string, 'utf8');
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(
data as unknown as Buffer,
fileName,
mimeType,
);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html
if (operation === 'delete') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileKey = this.getNodeParameter('fileKey', i) as string;
const options = this.getNodeParameter('options', i);
if (options.versionId) {
qs.versionId = options.versionId as string;
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'DELETE',
`/${fileKey}`,
'',
qs,
{},
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'getAll') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const options = this.getNodeParameter('options', 0);
if (options.folderKey) {
qs.prefix = options.folderKey as string;
}
if (options.fetchOwner) {
qs['fetch-owner'] = options.fetchOwner as string;
}
qs.delimiter = '/';
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
responseData = responseData.splice(0, qs.limit);
}
if (Array.isArray(responseData)) {
responseData = responseData.filter(
(e: IDataObject) => !(e.Key as string).endsWith('/') && e.Size !== '0',
);
if (qs.limit) {
responseData = responseData.splice(0, qs.limit as number);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
if (operation === 'upload') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileName = this.getNodeParameter('fileName', i) as string;
const isBinaryData = this.getNodeParameter('binaryData', i);
const additionalFields = this.getNodeParameter('additionalFields', i);
const tagsValues = (this.getNodeParameter('tagsUi', i) as IDataObject)
.tagsValues as IDataObject[];
let path = '/';
let body;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.parentFolderKey) {
path = `/${additionalFields.parentFolderKey}/`;
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
if (additionalFields.lockLegalHold) {
headers['x-amz-object-lock-legal-hold'] = (additionalFields.lockLegalHold as boolean)
? 'ON'
: 'OFF';
}
if (additionalFields.lockMode) {
headers['x-amz-object-lock-mode'] = (
additionalFields.lockMode as string
).toUpperCase();
}
if (additionalFields.lockRetainUntilDate) {
headers['x-amz-object-lock-retain-until-date'] =
additionalFields.lockRetainUntilDate as string;
}
if (additionalFields.serverSideEncryption) {
headers['x-amz-server-side-encryption'] =
additionalFields.serverSideEncryption as string;
}
if (additionalFields.encryptionAwsKmsKeyId) {
headers['x-amz-server-side-encryption-aws-kms-key-id'] =
additionalFields.encryptionAwsKmsKeyId as string;
}
if (additionalFields.serverSideEncryptionContext) {
headers['x-amz-server-side-encryption-context'] =
additionalFields.serverSideEncryptionContext as string;
}
if (additionalFields.serversideEncryptionCustomerAlgorithm) {
headers['x-amz-server-side-encryption-customer-algorithm'] =
additionalFields.serversideEncryptionCustomerAlgorithm as string;
}
if (additionalFields.serversideEncryptionCustomerKey) {
headers['x-amz-server-side-encryption-customer-key'] =
additionalFields.serversideEncryptionCustomerKey as string;
}
if (additionalFields.serversideEncryptionCustomerKeyMD5) {
headers['x-amz-server-side-encryption-customer-key-MD5'] =
additionalFields.serversideEncryptionCustomerKeyMD5 as string;
}
if (tagsValues) {
const tags: string[] = [];
tagsValues.forEach((o: IDataObject) => {
tags.push(`${o.key}=${o.value}`);
});
headers['x-amz-tagging'] = tags.join('&');
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (isBinaryData) {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const binaryPropertyData = this.helpers.assertBinaryData(i, binaryPropertyName);
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
i,
binaryPropertyName,
);
body = binaryDataBuffer;
headers['Content-Type'] = binaryPropertyData.mimeType;
headers['Content-MD5'] = createHash('md5').update(body).digest('base64');
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
`${path}${fileName || binaryPropertyData.fileName}`,
body,
qs,
headers,
{},
region as string,
);
} else {
const fileContent = this.getNodeParameter('fileContent', i) as string;
body = Buffer.from(fileContent, 'utf8');
headers['Content-Type'] = 'text/html';
headers['Content-MD5'] = createHash('md5').update(fileContent).digest('base64');
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
`${path}${fileName}`,
body,
qs,
headers,
{},
region as string,
);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
} catch (error) {
if (this.continueOnFail()) {
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ error: error.message }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
continue;
}
throw error;
}
}
if (resource === 'file' && operation === 'download') {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
return this.prepareOutputData(returnData);
}
} }
} }

View file

@ -0,0 +1,915 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import { paramCase, snakeCase } from 'change-case';
import { createHash } from 'crypto';
import { Builder } from 'xml2js';
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { bucketFields, bucketOperations } from './BucketDescription';
import { folderFields, folderOperations } from './FolderDescription';
import { fileFields, fileOperations } from './FileDescription';
import {
awsApiRequestREST,
awsApiRequestSOAP,
awsApiRequestSOAPAllItems,
} from './GenericFunctions';
export class AwsS3V1 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
displayName: 'AWS S3',
name: 'awsS3',
icon: 'file:s3.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Sends data to AWS S3',
defaults: {
name: 'AWS S3',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Bucket',
value: 'bucket',
},
{
name: 'File',
value: 'file',
},
{
name: 'Folder',
value: 'folder',
},
],
default: 'file',
},
// BUCKET
...bucketOperations,
...bucketFields,
// FOLDER
...folderOperations,
...folderFields,
// UPLOAD
...fileOperations,
...fileFields,
],
};
}
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
const headers: IDataObject = {};
try {
if (resource === 'bucket') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
if (operation === 'create') {
const credentials = await this.getCredentials('aws');
const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.bucketObjectLockEnabled) {
headers['x-amz-bucket-object-lock-enabled'] =
additionalFields.bucketObjectLockEnabled as boolean;
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWrite) {
headers['x-amz-grant-write'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
let region = credentials.region as string;
if (additionalFields.region) {
region = additionalFields.region as string;
}
const body: IDataObject = {
CreateBucketConfiguration: {
$: {
xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/',
},
},
};
let data = '';
// if credentials has the S3 defaul region (us-east-1) the body (XML) does not have to be sent.
if (region !== 'us-east-1') {
// @ts-ignore
body.CreateBucketConfiguration.LocationConstraint = [region];
const builder = new Builder();
data = builder.buildObject(body);
}
responseData = await awsApiRequestSOAP.call(
this,
`${name}.s3`,
'PUT',
'',
data,
qs,
headers,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html
if (operation === 'delete') {
const name = this.getNodeParameter('name', i) as string;
responseData = await awsApiRequestSOAP.call(
this,
`${name}.s3`,
'DELETE',
'',
'',
{},
headers,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', 0);
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListAllMyBucketsResult.Buckets.Bucket',
's3',
'GET',
'',
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListAllMyBucketsResult.Buckets.Bucket',
's3',
'GET',
'',
'',
qs,
);
responseData = responseData.slice(0, qs.limit);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'search') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
if (additionalFields.prefix) {
qs.prefix = additionalFields.prefix as string;
}
if (additionalFields.encodingType) {
qs['encoding-type'] = additionalFields.encodingType as string;
}
if (additionalFields.delimiter) {
qs.delimiter = additionalFields.delimiter as string;
}
if (additionalFields.fetchOwner) {
qs['fetch-owner'] = additionalFields.fetchOwner as string;
}
if (additionalFields.startAfter) {
qs['start-after'] = additionalFields.startAfter as string;
}
if (additionalFields.requesterPays) {
qs['x-amz-request-payer'] = 'requester';
}
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._ as string;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region,
);
} else {
qs['max-keys'] = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region,
);
responseData = responseData.ListBucketResult.Contents;
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
if (resource === 'folder') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
if (operation === 'create') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const folderName = this.getNodeParameter('folderName', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
let path = `/${folderName}/`;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.parentFolderKey) {
path = `/${additionalFields.parentFolderKey}${folderName}/`;
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
path,
'',
qs,
headers,
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html
if (operation === 'delete') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const folderKey = this.getNodeParameter('folderKey', i) as string;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'/',
'',
{ 'list-type': 2, prefix: folderKey },
{},
{},
region as string,
);
// folder empty then just delete it
if (responseData.length === 0) {
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'DELETE',
`/${folderKey}`,
'',
qs,
{},
{},
region as string,
);
responseData = { deleted: [{ Key: folderKey }] };
} else {
// delete everything inside the folder
const body: IDataObject = {
Delete: {
$: {
xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/',
},
Object: [],
},
};
for (const childObject of responseData) {
//@ts-ignore
(body.Delete.Object as IDataObject[]).push({
Key: childObject.Key as string,
});
}
const builder = new Builder();
const data = builder.buildObject(body);
headers['Content-MD5'] = createHash('md5').update(data).digest('base64');
headers['Content-Type'] = 'application/xml';
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'POST',
'/',
data,
{ delete: '' },
headers,
{},
region as string,
);
responseData = { deleted: responseData.DeleteResult.Deleted };
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'getAll') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const options = this.getNodeParameter('options', 0);
if (options.folderKey) {
qs.prefix = options.folderKey as string;
}
if (options.fetchOwner) {
qs['fetch-owner'] = options.fetchOwner as string;
}
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
}
if (Array.isArray(responseData)) {
responseData = responseData.filter(
(e: IDataObject) =>
(e.Key as string).endsWith('/') && e.Size === '0' && e.Key !== options.folderKey,
);
if (qs.limit) {
responseData = responseData.splice(0, qs.limit as number);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
}
if (resource === 'file') {
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html
if (operation === 'copy') {
const sourcePath = this.getNodeParameter('sourcePath', i) as string;
const destinationPath = this.getNodeParameter('destinationPath', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i);
headers['x-amz-copy-source'] = sourcePath;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
if (additionalFields.lockLegalHold) {
headers['x-amz-object-lock-legal-hold'] = (additionalFields.lockLegalHold as boolean)
? 'ON'
: 'OFF';
}
if (additionalFields.lockMode) {
headers['x-amz-object-lock-mode'] = (
additionalFields.lockMode as string
).toUpperCase();
}
if (additionalFields.lockRetainUntilDate) {
headers['x-amz-object-lock-retain-until-date'] =
additionalFields.lockRetainUntilDate as string;
}
if (additionalFields.serverSideEncryption) {
headers['x-amz-server-side-encryption'] =
additionalFields.serverSideEncryption as string;
}
if (additionalFields.encryptionAwsKmsKeyId) {
headers['x-amz-server-side-encryption-aws-kms-key-id'] =
additionalFields.encryptionAwsKmsKeyId as string;
}
if (additionalFields.serverSideEncryptionContext) {
headers['x-amz-server-side-encryption-context'] =
additionalFields.serverSideEncryptionContext as string;
}
if (additionalFields.serversideEncryptionCustomerAlgorithm) {
headers['x-amz-server-side-encryption-customer-algorithm'] =
additionalFields.serversideEncryptionCustomerAlgorithm as string;
}
if (additionalFields.serversideEncryptionCustomerKey) {
headers['x-amz-server-side-encryption-customer-key'] =
additionalFields.serversideEncryptionCustomerKey as string;
}
if (additionalFields.serversideEncryptionCustomerKeyMD5) {
headers['x-amz-server-side-encryption-customer-key-MD5'] =
additionalFields.serversideEncryptionCustomerKeyMD5 as string;
}
if (additionalFields.taggingDirective) {
headers['x-amz-tagging-directive'] = (
additionalFields.taggingDirective as string
).toUpperCase();
}
if (additionalFields.metadataDirective) {
headers['x-amz-metadata-directive'] = (
additionalFields.metadataDirective as string
).toUpperCase();
}
const destinationParts = destinationPath.split('/');
const bucketName = destinationParts[1];
const destination = `/${destinationParts.slice(2, destinationParts.length).join('/')}`;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
destination,
'',
qs,
headers,
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData.CopyObjectResult as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html
if (operation === 'download') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileKey = this.getNodeParameter('fileKey', i) as string;
const fileName = fileKey.split('/')[fileKey.split('/').length - 1];
if (fileKey.substring(fileKey.length - 1) === '/') {
throw new NodeOperationError(
this.getNode(),
'Downloading a whole directory is not yet supported, please provide a file key',
);
}
let region = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
region = region.LocationConstraint._;
const response = await awsApiRequestREST.call(
this,
`${bucketName}.s3`,
'GET',
`/${fileKey}`,
'',
qs,
{},
{ encoding: null, resolveWithFullResponse: true },
region as string,
);
let mimeType: string | undefined;
if (response.headers['content-type']) {
mimeType = response.headers['content-type'];
}
const newItem: INodeExecutionData = {
json: items[i].json,
binary: {},
};
if (items[i].binary !== undefined && newItem.binary) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, items[i].binary);
}
items[i] = newItem;
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
const data = Buffer.from(response.body as string, 'utf8');
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(
data as unknown as Buffer,
fileName,
mimeType,
);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html
if (operation === 'delete') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileKey = this.getNodeParameter('fileKey', i) as string;
const options = this.getNodeParameter('options', i);
if (options.versionId) {
qs.versionId = options.versionId as string;
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'DELETE',
`/${fileKey}`,
'',
qs,
{},
{},
region as string,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
if (operation === 'getAll') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const returnAll = this.getNodeParameter('returnAll', 0);
const options = this.getNodeParameter('options', 0);
if (options.folderKey) {
qs.prefix = options.folderKey as string;
}
if (options.fetchOwner) {
qs['fetch-owner'] = options.fetchOwner as string;
}
qs.delimiter = '/';
qs['list-type'] = 2;
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (returnAll) {
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
} else {
qs.limit = this.getNodeParameter('limit', 0);
responseData = await awsApiRequestSOAPAllItems.call(
this,
'ListBucketResult.Contents',
`${bucketName}.s3`,
'GET',
'',
'',
qs,
{},
{},
region as string,
);
responseData = responseData.splice(0, qs.limit);
}
if (Array.isArray(responseData)) {
responseData = responseData.filter(
(e: IDataObject) => !(e.Key as string).endsWith('/') && e.Size !== '0',
);
if (qs.limit) {
responseData = responseData.splice(0, qs.limit as number);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
if (operation === 'upload') {
const bucketName = this.getNodeParameter('bucketName', i) as string;
const fileName = this.getNodeParameter('fileName', i) as string;
const isBinaryData = this.getNodeParameter('binaryData', i);
const additionalFields = this.getNodeParameter('additionalFields', i);
const tagsValues = (this.getNodeParameter('tagsUi', i) as IDataObject)
.tagsValues as IDataObject[];
let path = '/';
let body;
if (additionalFields.requesterPays) {
headers['x-amz-request-payer'] = 'requester';
}
if (additionalFields.parentFolderKey) {
path = `/${additionalFields.parentFolderKey}/`;
}
if (additionalFields.storageClass) {
headers['x-amz-storage-class'] = snakeCase(
additionalFields.storageClass as string,
).toUpperCase();
}
if (additionalFields.acl) {
headers['x-amz-acl'] = paramCase(additionalFields.acl as string);
}
if (additionalFields.grantFullControl) {
headers['x-amz-grant-full-control'] = '';
}
if (additionalFields.grantRead) {
headers['x-amz-grant-read'] = '';
}
if (additionalFields.grantReadAcp) {
headers['x-amz-grant-read-acp'] = '';
}
if (additionalFields.grantWriteAcp) {
headers['x-amz-grant-write-acp'] = '';
}
if (additionalFields.lockLegalHold) {
headers['x-amz-object-lock-legal-hold'] = (additionalFields.lockLegalHold as boolean)
? 'ON'
: 'OFF';
}
if (additionalFields.lockMode) {
headers['x-amz-object-lock-mode'] = (
additionalFields.lockMode as string
).toUpperCase();
}
if (additionalFields.lockRetainUntilDate) {
headers['x-amz-object-lock-retain-until-date'] =
additionalFields.lockRetainUntilDate as string;
}
if (additionalFields.serverSideEncryption) {
headers['x-amz-server-side-encryption'] =
additionalFields.serverSideEncryption as string;
}
if (additionalFields.encryptionAwsKmsKeyId) {
headers['x-amz-server-side-encryption-aws-kms-key-id'] =
additionalFields.encryptionAwsKmsKeyId as string;
}
if (additionalFields.serverSideEncryptionContext) {
headers['x-amz-server-side-encryption-context'] =
additionalFields.serverSideEncryptionContext as string;
}
if (additionalFields.serversideEncryptionCustomerAlgorithm) {
headers['x-amz-server-side-encryption-customer-algorithm'] =
additionalFields.serversideEncryptionCustomerAlgorithm as string;
}
if (additionalFields.serversideEncryptionCustomerKey) {
headers['x-amz-server-side-encryption-customer-key'] =
additionalFields.serversideEncryptionCustomerKey as string;
}
if (additionalFields.serversideEncryptionCustomerKeyMD5) {
headers['x-amz-server-side-encryption-customer-key-MD5'] =
additionalFields.serversideEncryptionCustomerKeyMD5 as string;
}
if (tagsValues) {
const tags: string[] = [];
tagsValues.forEach((o: IDataObject) => {
tags.push(`${o.key}=${o.value}`);
});
headers['x-amz-tagging'] = tags.join('&');
}
responseData = await awsApiRequestSOAP.call(this, `${bucketName}.s3`, 'GET', '', '', {
location: '',
});
const region = responseData.LocationConstraint._;
if (isBinaryData) {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const binaryPropertyData = this.helpers.assertBinaryData(i, binaryPropertyName);
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
i,
binaryPropertyName,
);
body = binaryDataBuffer;
headers['Content-Type'] = binaryPropertyData.mimeType;
headers['Content-MD5'] = createHash('md5').update(body).digest('base64');
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
`${path}${fileName || binaryPropertyData.fileName}`,
body,
qs,
headers,
{},
region as string,
);
} else {
const fileContent = this.getNodeParameter('fileContent', i) as string;
body = Buffer.from(fileContent, 'utf8');
headers['Content-Type'] = 'text/html';
headers['Content-MD5'] = createHash('md5').update(fileContent).digest('base64');
responseData = await awsApiRequestSOAP.call(
this,
`${bucketName}.s3`,
'PUT',
`${path}${fileName}`,
body,
qs,
headers,
{},
region as string,
);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ success: true }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
}
} catch (error) {
if (this.continueOnFail()) {
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ error: error.message }),
{ itemData: { item: i } },
);
returnData.push(...executionData);
continue;
}
throw error;
}
}
if (resource === 'file' && operation === 'download') {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
return this.prepareOutputData(returnData);
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,321 @@
import type { INodeProperties } from 'n8n-workflow';
export const bucketOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['bucket'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a bucket',
action: 'Create a bucket',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a bucket',
action: 'Delete a bucket',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many buckets',
action: 'Get many buckets',
},
{
name: 'Search',
value: 'search',
description: 'Search within a bucket',
action: 'Search a bucket',
},
],
default: 'create',
},
];
export const bucketFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* bucket:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['bucket'],
operation: ['create'],
},
},
description: 'A succinct description of the nature, symptoms, cause, or effect of the bucket',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: ['bucket'],
operation: ['create'],
},
},
default: {},
options: [
{
displayName: 'ACL',
name: 'acl',
type: 'options',
options: [
{
name: 'Authenticated Read',
value: 'authenticatedRead',
},
{
name: 'Private',
value: 'Private',
},
{
name: 'Public Read',
value: 'publicRead',
},
{
name: 'Public Read Write',
value: 'publicReadWrite',
},
],
default: '',
description: 'The canned ACL to apply to the bucket',
},
{
displayName: 'Bucket Object Lock Enabled',
name: 'bucketObjectLockEnabled',
type: 'boolean',
default: false,
description: 'Whether you want S3 Object Lock to be enabled for the new bucket',
},
{
displayName: 'Grant Full Control',
name: 'grantFullControl',
type: 'boolean',
default: false,
description:
'Whether to allow grantee the read, write, read ACP, and write ACP permissions on the bucket',
},
{
displayName: 'Grant Read',
name: 'grantRead',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to list the objects in the bucket',
},
{
displayName: 'Grant Read ACP',
name: 'grantReadAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to read the bucket ACL',
},
{
displayName: 'Grant Write',
name: 'grantWrite',
type: 'boolean',
default: false,
description:
'Whether to allow grantee to create, overwrite, and delete any object in the bucket',
},
{
displayName: 'Grant Write ACP',
name: 'grantWriteAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to write the ACL for the applicable bucket',
},
{
displayName: 'Region',
name: 'region',
type: 'string',
default: '',
description:
'Region you want to create the bucket in, by default the buckets are created on the region defined on the credentials',
},
],
},
/* -------------------------------------------------------------------------- */
/* bucket:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['bucket'],
operation: ['delete'],
},
},
description: 'Name of the AWS S3 bucket to delete',
},
/* -------------------------------------------------------------------------- */
/* bucket:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['bucket'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['bucket'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'Max number of results to return',
},
/* -------------------------------------------------------------------------- */
/* bucket:search */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['bucket'],
operation: ['search'],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['search'],
resource: ['bucket'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: ['search'],
resource: ['bucket'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'Max number of results to return',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: ['bucket'],
operation: ['search'],
},
},
default: {},
options: [
{
displayName: 'Delimiter',
name: 'delimiter',
type: 'string',
default: '',
description: 'A delimiter is a character you use to group keys',
},
{
displayName: 'Encoding Type',
name: 'encodingType',
type: 'options',
options: [
{
name: 'URL',
value: 'url',
},
],
default: '',
description: 'Encoding type used by Amazon S3 to encode object keys in the response',
},
{
displayName: 'Fetch Owner',
name: 'fetchOwner',
type: 'boolean',
default: false,
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
description:
'The owner field is not present in listV2 by default, if you want to return owner field with each key in the result then set the fetch owner field to true',
},
{
displayName: 'Prefix',
name: 'prefix',
type: 'string',
default: '',
description: 'Limits the response to keys that begin with the specified prefix',
},
{
displayName: 'Requester Pays',
name: 'requesterPays',
type: 'boolean',
default: false,
description:
'Whether the requester will pay for requests and data transfer. While Requester Pays is enabled, anonymous access to this bucket is disabled.',
},
{
displayName: 'Start After',
name: 'startAfter',
type: 'string',
default: '',
description:
'StartAfter is where you want Amazon S3 to start listing from. Amazon S3 starts listing after this specified key.',
},
],
},
];

View file

@ -0,0 +1,841 @@
import type { INodeProperties } from 'n8n-workflow';
export const fileOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['file'],
},
},
options: [
{
name: 'Copy',
value: 'copy',
description: 'Copy a file',
action: 'Copy a file',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a file',
action: 'Delete a file',
},
{
name: 'Download',
value: 'download',
description: 'Download a file',
action: 'Download a file',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many files',
action: 'Get many files',
},
{
name: 'Upload',
value: 'upload',
description: 'Upload a file',
action: 'Upload a file',
},
],
default: 'download',
},
];
export const fileFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* file:copy */
/* -------------------------------------------------------------------------- */
{
displayName: 'Source Path',
name: 'sourcePath',
type: 'string',
required: true,
default: '',
placeholder: '/bucket/my-image.jpg',
displayOptions: {
show: {
resource: ['file'],
operation: ['copy'],
},
},
description:
'The name of the source bucket and key name of the source object, separated by a slash (/)',
},
{
displayName: 'Destination Path',
name: 'destinationPath',
type: 'string',
required: true,
default: '',
placeholder: '/bucket/my-second-image.jpg',
displayOptions: {
show: {
resource: ['file'],
operation: ['copy'],
},
},
description:
'The name of the destination bucket and key name of the destination object, separated by a slash (/)',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: ['file'],
operation: ['copy'],
},
},
default: {},
options: [
{
displayName: 'ACL',
name: 'acl',
type: 'options',
options: [
{
name: 'Authenticated Read',
value: 'authenticatedRead',
},
{
name: 'AWS Exec Read',
value: 'awsExecRead',
},
{
name: 'Bucket Owner Full Control',
value: 'bucketOwnerFullControl',
},
{
name: 'Bucket Owner Read',
value: 'bucketOwnerRead',
},
{
name: 'Private',
value: 'private',
},
{
name: 'Public Read',
value: 'publicRead',
},
{
name: 'Public Read Write',
value: 'publicReadWrite',
},
],
default: 'private',
description: 'The canned ACL to apply to the object',
},
{
displayName: 'Grant Full Control',
name: 'grantFullControl',
type: 'boolean',
default: false,
description:
'Whether to give the grantee READ, READ_ACP, and WRITE_ACP permissions on the object',
},
{
displayName: 'Grant Read',
name: 'grantRead',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to read the object data and its metadata',
},
{
displayName: 'Grant Read ACP',
name: 'grantReadAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to read the object ACL',
},
{
displayName: 'Grant Write ACP',
name: 'grantWriteAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to write the ACL for the applicable object',
},
{
displayName: 'Lock Legal Hold',
name: 'lockLegalHold',
type: 'boolean',
default: false,
description: 'Whether a legal hold will be applied to this object',
},
{
displayName: 'Lock Mode',
name: 'lockMode',
type: 'options',
options: [
{
name: 'Governance',
value: 'governance',
},
{
name: 'Compliance',
value: 'compliance',
},
],
default: '',
description: 'The Object Lock mode that you want to apply to this object',
},
{
displayName: 'Lock Retain Until Date',
name: 'lockRetainUntilDate',
type: 'dateTime',
default: '',
description: "The date and time when you want this object's Object Lock to expire",
},
{
displayName: 'Metadata Directive',
name: 'metadataDirective',
type: 'options',
options: [
{
name: 'Copy',
value: 'copy',
},
{
name: 'Replace',
value: 'replace',
},
],
default: '',
description:
'Specifies whether the metadata is copied from the source object or replaced with metadata provided in the request',
},
{
displayName: 'Requester Pays',
name: 'requesterPays',
type: 'boolean',
default: false,
description:
'Whether the requester will pay for requests and data transfer. While Requester Pays is enabled, anonymous access to this bucket is disabled.',
},
{
displayName: 'Server Side Encryption',
name: 'serverSideEncryption',
type: 'options',
options: [
{
name: 'AES256',
value: 'AES256',
},
{
name: 'AWS:KMS',
value: 'aws:kms',
},
],
default: '',
description:
'The server-side encryption algorithm used when storing this object in Amazon S3',
},
{
displayName: 'Server Side Encryption Context',
name: 'serverSideEncryptionContext',
type: 'string',
default: '',
description: 'Specifies the AWS KMS Encryption Context to use for object encryption',
},
{
displayName: 'Server Side Encryption AWS KMS Key ID',
name: 'encryptionAwsKmsKeyId',
type: 'string',
default: '',
description: 'If x-amz-server-side-encryption is present and has the value of aws:kms',
},
{
displayName: 'Server Side Encryption Customer Algorithm',
name: 'serversideEncryptionCustomerAlgorithm',
type: 'string',
default: '',
description:
'Specifies the algorithm to use to when encrypting the object (for example, AES256)',
},
{
displayName: 'Server Side Encryption Customer Key',
name: 'serversideEncryptionCustomerKey',
type: 'string',
default: '',
description:
'Specifies the customer-provided encryption key for Amazon S3 to use in encrypting data',
},
{
displayName: 'Server Side Encryption Customer Key MD5',
name: 'serversideEncryptionCustomerKeyMD5',
type: 'string',
default: '',
description: 'Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321',
},
{
displayName: 'Storage Class',
name: 'storageClass',
type: 'options',
options: [
{
name: 'Deep Archive',
value: 'deepArchive',
},
{
name: 'Glacier',
value: 'glacier',
},
{
name: 'Intelligent Tiering',
value: 'intelligentTiering',
},
{
name: 'One Zone IA',
value: 'onezoneIA',
},
{
name: 'Standard',
value: 'standard',
},
{
name: 'Standard IA',
value: 'standardIA',
},
],
default: 'standard',
description: 'Amazon S3 storage classes',
},
{
displayName: 'Tagging Directive',
name: 'taggingDirective',
type: 'options',
options: [
{
name: 'Copy',
value: 'copy',
},
{
name: 'Replace',
value: 'replace',
},
],
default: '',
description:
'Specifies whether the metadata is copied from the source object or replaced with metadata provided in the request',
},
],
},
/* -------------------------------------------------------------------------- */
/* file:upload */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
},
},
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
placeholder: 'hello.txt',
required: true,
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
binaryData: [false],
},
},
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
binaryData: [true],
},
},
description: 'If not set the binary data filename will be used',
},
{
displayName: 'Binary Data',
name: 'binaryData',
type: 'boolean',
default: true,
displayOptions: {
show: {
operation: ['upload'],
resource: ['file'],
},
},
description: 'Whether the data to upload should be taken from binary field',
},
{
displayName: 'File Content',
name: 'fileContent',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['upload'],
resource: ['file'],
binaryData: [false],
},
},
placeholder: '',
description: 'The text content of the file to upload',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
operation: ['upload'],
resource: ['file'],
binaryData: [true],
},
},
placeholder: '',
description: 'Name of the binary property which contains the data for the file to be uploaded',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
},
},
default: {},
options: [
{
displayName: 'ACL',
name: 'acl',
type: 'options',
options: [
{
name: 'Authenticated Read',
value: 'authenticatedRead',
},
{
name: 'AWS Exec Read',
value: 'awsExecRead',
},
{
name: 'Bucket Owner Full Control',
value: 'bucketOwnerFullControl',
},
{
name: 'Bucket Owner Read',
value: 'bucketOwnerRead',
},
{
name: 'Private',
value: 'private',
},
{
name: 'Public Read',
value: 'publicRead',
},
{
name: 'Public Read Write',
value: 'publicReadWrite',
},
],
default: 'private',
description: 'The canned ACL to apply to the object',
},
{
displayName: 'Grant Full Control',
name: 'grantFullControl',
type: 'boolean',
default: false,
description:
'Whether to give the grantee READ, READ_ACP, and WRITE_ACP permissions on the object',
},
{
displayName: 'Grant Read',
name: 'grantRead',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to read the object data and its metadata',
},
{
displayName: 'Grant Read ACP',
name: 'grantReadAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to read the object ACL',
},
{
displayName: 'Grant Write ACP',
name: 'grantWriteAcp',
type: 'boolean',
default: false,
description: 'Whether to allow grantee to write the ACL for the applicable object',
},
{
displayName: 'Lock Legal Hold',
name: 'lockLegalHold',
type: 'boolean',
default: false,
description: 'Whether a legal hold will be applied to this object',
},
{
displayName: 'Lock Mode',
name: 'lockMode',
type: 'options',
options: [
{
name: 'Governance',
value: 'governance',
},
{
name: 'Compliance',
value: 'compliance',
},
],
default: '',
description: 'The Object Lock mode that you want to apply to this object',
},
{
displayName: 'Lock Retain Until Date',
name: 'lockRetainUntilDate',
type: 'dateTime',
default: '',
description: "The date and time when you want this object's Object Lock to expire",
},
{
displayName: 'Parent Folder Key',
name: 'parentFolderKey',
type: 'string',
default: '',
description: 'Parent folder you want to create the file in',
},
{
displayName: 'Requester Pays',
name: 'requesterPays',
type: 'boolean',
default: false,
description:
'Whether the requester will pay for requests and data transfer. While Requester Pays is enabled, anonymous access to this bucket is disabled.',
},
{
displayName: 'Server Side Encryption',
name: 'serverSideEncryption',
type: 'options',
options: [
{
name: 'AES256',
value: 'AES256',
},
{
name: 'AWS:KMS',
value: 'aws:kms',
},
],
default: '',
description:
'The server-side encryption algorithm used when storing this object in Amazon S3',
},
{
displayName: 'Server Side Encryption Context',
name: 'serverSideEncryptionContext',
type: 'string',
default: '',
description: 'Specifies the AWS KMS Encryption Context to use for object encryption',
},
{
displayName: 'Server Side Encryption AWS KMS Key ID',
name: 'encryptionAwsKmsKeyId',
type: 'string',
default: '',
description: 'If x-amz-server-side-encryption is present and has the value of aws:kms',
},
{
displayName: 'Server Side Encryption Customer Algorithm',
name: 'serversideEncryptionCustomerAlgorithm',
type: 'string',
default: '',
description:
'Specifies the algorithm to use to when encrypting the object (for example, AES256)',
},
{
displayName: 'Server Side Encryption Customer Key',
name: 'serversideEncryptionCustomerKey',
type: 'string',
default: '',
description:
'Specifies the customer-provided encryption key for Amazon S3 to use in encrypting data',
},
{
displayName: 'Server Side Encryption Customer Key MD5',
name: 'serversideEncryptionCustomerKeyMD5',
type: 'string',
default: '',
description: 'Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321',
},
{
displayName: 'Storage Class',
name: 'storageClass',
type: 'options',
options: [
{
name: 'Deep Archive',
value: 'deepArchive',
},
{
name: 'Glacier',
value: 'glacier',
},
{
name: 'Intelligent Tiering',
value: 'intelligentTiering',
},
{
name: 'One Zone IA',
value: 'onezoneIA',
},
{
name: 'Standard',
value: 'standard',
},
{
name: 'Standard IA',
value: 'standardIA',
},
],
default: 'standard',
description: 'Amazon S3 storage classes',
},
],
},
{
displayName: 'Tags',
name: 'tagsUi',
placeholder: 'Add Tag',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
},
},
options: [
{
name: 'tagsValues',
displayName: 'Tag',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
],
},
],
description: 'Optional extra headers to add to the message (most headers are allowed)',
},
/* -------------------------------------------------------------------------- */
/* file:download */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['download'],
},
},
},
{
displayName: 'File Key',
name: 'fileKey',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['download'],
},
},
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
required: true,
default: 'data',
displayOptions: {
show: {
operation: ['download'],
resource: ['file'],
},
},
description: 'Name of the binary property to which to write the data of the read file',
},
/* -------------------------------------------------------------------------- */
/* file:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['delete'],
},
},
},
{
displayName: 'File Key',
name: 'fileKey',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['delete'],
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['file'],
operation: ['delete'],
},
},
options: [
{
displayName: 'Version ID',
name: 'versionId',
type: 'string',
default: '',
},
],
},
/* -------------------------------------------------------------------------- */
/* file:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['file'],
operation: ['getAll'],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['file'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['file'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'Max number of results to return',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['file'],
operation: ['getAll'],
},
},
options: [
{
displayName: 'Fetch Owner',
name: 'fetchOwner',
type: 'boolean',
default: false,
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
description:
'The owner field is not present in listV2 by default, if you want to return owner field with each key in the result then set the fetch owner field to true',
},
{
displayName: 'Folder Key',
name: 'folderKey',
type: 'string',
default: '',
},
],
},
];

View file

@ -0,0 +1,241 @@
import type { INodeProperties } from 'n8n-workflow';
export const folderOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['folder'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a folder',
action: 'Create a folder',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a folder',
action: 'Delete a folder',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many folders',
action: 'Get many folders',
},
],
default: 'create',
},
];
export const folderFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* folder:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['folder'],
operation: ['create'],
},
},
},
{
displayName: 'Folder Name',
name: 'folderName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['folder'],
operation: ['create'],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: ['folder'],
operation: ['create'],
},
},
default: {},
options: [
{
displayName: 'Parent Folder Key',
name: 'parentFolderKey',
type: 'string',
default: '',
description: 'Parent folder you want to create the folder in',
},
{
displayName: 'Requester Pays',
name: 'requesterPays',
type: 'boolean',
default: false,
description:
'Whether the requester will pay for requests and data transfer. While Requester Pays is enabled, anonymous access to this bucket is disabled.',
},
{
displayName: 'Storage Class',
name: 'storageClass',
type: 'options',
options: [
{
name: 'Deep Archive',
value: 'deepArchive',
},
{
name: 'Glacier',
value: 'glacier',
},
{
name: 'Intelligent Tiering',
value: 'intelligentTiering',
},
{
name: 'One Zone IA',
value: 'onezoneIA',
},
{
name: 'Reduced Redundancy',
value: 'RecudedRedundancy',
},
{
name: 'Standard',
value: 'standard',
},
{
name: 'Standard IA',
value: 'standardIA',
},
],
default: 'standard',
description: 'Amazon S3 storage classes',
},
],
},
/* -------------------------------------------------------------------------- */
/* folder:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['folder'],
operation: ['delete'],
},
},
},
{
displayName: 'Folder Key',
name: 'folderKey',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['folder'],
operation: ['delete'],
},
},
},
/* -------------------------------------------------------------------------- */
/* folder:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Bucket Name',
name: 'bucketName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['folder'],
operation: ['getAll'],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['folder'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['folder'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'Max number of results to return',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['folder'],
operation: ['getAll'],
},
},
options: [
{
displayName: 'Fetch Owner',
name: 'fetchOwner',
type: 'boolean',
default: false,
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
description:
'The owner field is not present in listV2 by default, if you want to return owner field with each key in the result then set the fetch owner field to true',
},
{
displayName: 'Folder Key',
name: 'folderKey',
type: 'string',
default: '',
},
],
},
];

View file

@ -0,0 +1,132 @@
import get from 'lodash.get';
import { parseString } from 'xml2js';
import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
IHttpRequestOptions,
} from 'n8n-workflow';
export async function awsApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
service: string,
method: string,
path: string,
body?: string | Buffer | any,
query: IDataObject = {},
headers?: object,
option: IDataObject = {},
_region?: string,
): Promise<any> {
const requestOptions = {
qs: {
...query,
service,
path,
query,
},
method,
body,
url: '',
headers,
} as IHttpRequestOptions;
if (Object.keys(option).length !== 0) {
Object.assign(requestOptions, option);
}
return this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
}
export async function awsApiRequestREST(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
service: string,
method: string,
path: string,
body?: string | Buffer | any,
query: IDataObject = {},
headers?: object,
options: IDataObject = {},
region?: string,
): Promise<any> {
const response = await awsApiRequest.call(
this,
service,
method,
path,
body,
query,
headers,
options,
region,
);
try {
if (response.includes('<?xml version="1.0" encoding="UTF-8"?>')) {
return await new Promise((resolve, reject) => {
parseString(response as string, { explicitArray: false }, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
return JSON.parse(response as string);
} catch (error) {
return response;
}
}
export async function awsApiRequestRESTAllItems(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
service: string,
method: string,
path: string,
body?: string,
query: IDataObject = {},
headers?: object,
option: IDataObject = {},
region?: string,
): Promise<any> {
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await awsApiRequestREST.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`)) {
query['continuation-token'] = get(
responseData,
`${propertyName.split('.')[0]}.NextContinuationToken`,
);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
returnData.push.apply(returnData, get(responseData, propertyName) as IDataObject[]);
} else {
returnData.push(get(responseData, propertyName) as IDataObject);
}
}
const limit = query.limit as number | undefined;
if (limit && limit <= returnData.length) {
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
);
return returnData;
}

View file

@ -3,7 +3,7 @@ import { getWorkflowFilenames, initBinaryDataManager, testWorkflows } from '@tes
const workflows = getWorkflowFilenames(__dirname); const workflows = getWorkflowFilenames(__dirname);
describe('Test S3 Node', () => { describe('Test S3 V1 Node', () => {
describe('File Upload', () => { describe('File Upload', () => {
let mock: nock.Scope; let mock: nock.Scope;
const now = 1683028800000; const now = 1683028800000;

View file

@ -0,0 +1,97 @@
{
"name": "Test S3 upload",
"nodes": [
{
"parameters": {},
"id": "8f35d24b-1493-43a4-846f-bacb577bfcb2",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [540, 340]
},
{
"parameters": {
"mode": "jsonToBinary",
"options": {}
},
"id": "eae2946a-1a1e-47e9-9fd6-e32119b13ec0",
"name": "Move Binary Data",
"type": "n8n-nodes-base.moveBinaryData",
"typeVersion": 1,
"position": [900, 340]
},
{
"parameters": {
"operation": "upload",
"bucketName": "bucket",
"fileName": "binary.json",
"additionalFields": {}
},
"id": "6f21fa3f-ede1-44b1-8182-a2c07152f666",
"name": "AWS S3",
"type": "n8n-nodes-base.awsS3",
"typeVersion": 2,
"position": [1080, 340],
"credentials": {
"aws": {
"id": "1",
"name": "AWS account"
}
}
},
{
"parameters": {
"jsCode": "return [{ key: \"value\" }];"
},
"id": "e12f1876-cfd1-47a4-a21b-d478452683bc",
"name": "Code",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [720, 340]
}
],
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
}
]
]
},
"Move Binary Data": {
"main": [
[
{
"node": "AWS S3",
"type": "main",
"index": 0
}
]
]
},
"Code": {
"main": [
[
{
"node": "Move Binary Data",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {
"AWS S3": [
{
"json": {
"success": true
}
}
]
}
}

View file

@ -0,0 +1,48 @@
import nock from 'nock';
import { getWorkflowFilenames, initBinaryDataManager, testWorkflows } from '@test/nodes/Helpers';
const workflows = getWorkflowFilenames(__dirname);
describe('Test S3 V2 Node', () => {
describe('File Upload', () => {
let mock: nock.Scope;
const now = 1683028800000;
beforeAll(async () => {
jest.useFakeTimers({ doNotFake: ['nextTick'], now });
await initBinaryDataManager();
nock.disableNetConnect();
mock = nock('https://bucket.s3.eu-central-1.amazonaws.com');
});
beforeEach(async () => {
mock.get('/?location').reply(
200,
`<?xml version="1.0" encoding="UTF-8"?>
<LocationConstraint>
<LocationConstraint>eu-central-1</LocationConstraint>
</LocationConstraint>`,
{
'content-type': 'application/xml',
},
);
mock
.put('/binary.json')
.matchHeader(
'X-Amz-Content-Sha256',
'e43abcf3375244839c012f9633f95862d232a95b00d5bc7348b3098b9fed7f32',
)
.once()
.reply(200, { success: true });
});
afterAll(() => {
nock.restore();
});
testWorkflows(workflows);
});
});

View file

@ -14,11 +14,11 @@ import type {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeApiError, NodeOperationError } from 'n8n-workflow'; import { NodeApiError, NodeOperationError } from 'n8n-workflow';
import { bucketFields, bucketOperations } from '../Aws/S3/BucketDescription'; import { bucketFields, bucketOperations } from '../Aws/S3/V1/BucketDescription';
import { folderFields, folderOperations } from '../Aws/S3/FolderDescription'; import { folderFields, folderOperations } from '../Aws/S3/V1/FolderDescription';
import { fileFields, fileOperations } from '../Aws/S3/FileDescription'; import { fileFields, fileOperations } from '../Aws/S3/V1/FileDescription';
import { s3ApiRequestREST, s3ApiRequestSOAP, s3ApiRequestSOAPAllItems } from './GenericFunctions'; import { s3ApiRequestREST, s3ApiRequestSOAP, s3ApiRequestSOAPAllItems } from './GenericFunctions';