mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
d6239d5bfb
* Update Compression node * Update Crypto node * Update DateTime node * Update EditImage node * Update EmailSend node * Update ExecuteWorkflow node * Update FTP node * Update Function node * Update FunctionItem node * Update ExecuteCommand node * Update OpenWeatherMap node * Update ReadBinaryFile node * Update ReadPdf node * Update RssFeedRead node & add URL validation * Update SpreadsheetFile node * Update Switch node * Update WriteBinaryFile node * Update Xml node * Update ActiveCampaign node * Update Airtable node * Update ApiTemplateIo node * Update Asana node * Update AwsLambda node * Update AwsSns node * Update AwsComprehend node * Update AwsRekognition node * Update AwsS3 node * Fix Error item * Update AwsSes node * Update AwsSqs node * Update Amqp node * Update Bitly node * Update Box node * Update Brandfetch node * Update CircleCi node * Update Clearbit node * Update ClickUp node * Update Cockpit node * Update CoinGecko node * Update Contentful node * Update ConvertKit node * Update Cortex node * Update CustomerIo node * Update DeepL node * Update Demio node * Update Disqus node * Update Drift node * Update Dropbox node * Update GetResponse node * Refactor & Update Ghost node * Update Github node * Update Gitlab node * Update GoogleAnalytics node * Update GoogleBooks node * Update GoogleCalendar node * Update GoogleDrive node * Update Gmail node * Update GoogleSheets node * Update GoogleSlides node * Update GoogleTasks node * Update Gotify node * Update GraphQL node * Update HackerNews node * Update Harvest node * Update HtmlExtract node * Update Hubspot node * Update Hunter node * Update Intercom node * Update Kafka node * Refactor & update Line node * Update LinkedIn node * Update Mailchimp node * Update Mandrill node * Update Matrix node * Update Mautic node * Update Medium node * Update MessageBird node * Update Mindee node * Update Mocean node * Update MondayCom node * Update MicrosoftExcel node * Update MicrosoftOneDrive node * Update MicrosoftOutlook node * Update Affinity node * Update Chargebee node * Update Discourse node * Update Freshdesk node * Update YouTube node * Update InvoiceNinja node * Update MailerLite node * Update Mailgun node * Update Mailjet node * Update Mattermost node * Update Nasa node * Update NextCloud node * Update OpenThesaurus node * Update Orbit node * Update PagerDuty node * Update PayPal node * Update Peekalink node * Update Phantombuster node * Update PostHog node * Update ProfitWell node * Refactor & Update Pushbullet node * Update QuickBooks node * Update Raindrop node * Update Reddit node * Update Rocketchat node * Update S3 node * Update Salesforce node * Update SendGrid node * Update SentryIo node * Update Shopify node * Update Signl4 node * Update Slack node * Update Spontit node * Update Spotify node * Update Storyblok node * Refactor & Update Strapi node * Refactor & Update Strava node * Update Taiga node * Refactor & update Tapfiliate node * Update Telegram node * Update TheHive node * Update Todoist node * Update TravisCi node * Update Trello node * Update Twilio node * Update Twist node * Update Twitter node * Update Uplead node * Update UProc node * Update Vero node * Update Webflow node * Update Wekan node * Update Wordpress node * Update Xero node * Update Yourls node * Update Zendesk node * Update ZohoCrm node * Refactor & Update Zoom node * Update Zulip node * Update Clockify node * Update MongoDb node * Update MySql node * Update MicrosoftTeams node * Update Stackby node * Refactor Discourse node * Support corner-case in Github node update * Support corner-case in Gitlab node update * Refactor & Update GoogleContacts node * Refactor Mindee node * Update Coda node * Lint fixes * Update Beeminder node * Update Google Firebase RealtimeDatabase node * Update HelpScout node * Update Mailcheck node * Update Paddle node * Update Pipedrive node * Update Pushover node * Update Segment node * Refactor & Update Vonage node * Added new conditions to warnings on execute batch cmd * Added keep only properties flag * Fixed code for keep only props * Added dependencies for image editing Co-authored-by: dali <servfrdali@yahoo.fr>
655 lines
23 KiB
TypeScript
655 lines
23 KiB
TypeScript
|
|
import {
|
|
paramCase,
|
|
snakeCase,
|
|
} from 'change-case';
|
|
|
|
import {
|
|
createHash,
|
|
} from 'crypto';
|
|
|
|
import {
|
|
Builder,
|
|
} from 'xml2js';
|
|
|
|
import {
|
|
BINARY_ENCODING,
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IBinaryKeyData,
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
bucketFields,
|
|
bucketOperations,
|
|
} from '../Aws/S3/BucketDescription';
|
|
|
|
import {
|
|
folderFields,
|
|
folderOperations,
|
|
} from '../Aws/S3/FolderDescription';
|
|
|
|
import {
|
|
fileFields,
|
|
fileOperations,
|
|
} from '../Aws/S3/FileDescription';
|
|
|
|
import {
|
|
s3ApiRequestREST,
|
|
s3ApiRequestSOAP,
|
|
s3ApiRequestSOAPAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
export class S3 implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'S3',
|
|
name: 's3',
|
|
icon: 'file:s3.png',
|
|
group: ['output'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Sends data to any S3-compatible service',
|
|
defaults: {
|
|
name: 'S3',
|
|
color: '#d05b4b',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 's3',
|
|
required: true,
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Bucket',
|
|
value: 'bucket',
|
|
},
|
|
{
|
|
name: 'File',
|
|
value: 'file',
|
|
},
|
|
{
|
|
name: 'Folder',
|
|
value: 'folder',
|
|
},
|
|
],
|
|
default: 'file',
|
|
description: 'The operation to perform.',
|
|
},
|
|
// BUCKET
|
|
...bucketOperations,
|
|
...bucketFields,
|
|
// FOLDER
|
|
...folderOperations,
|
|
...folderFields,
|
|
// UPLOAD
|
|
...fileOperations,
|
|
...fileFields,
|
|
],
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const qs: IDataObject = {};
|
|
let responseData;
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
for (let i = 0; i < items.length; i++) {
|
|
try {
|
|
const headers: IDataObject = {};
|
|
if (resource === 'bucket') {
|
|
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
|
|
if (operation === 'create') {
|
|
|
|
let credentials;
|
|
|
|
try {
|
|
credentials = this.getCredentials('s3');
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
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 s3ApiRequestSOAP.call(this, `${name}`, 'PUT', '', data, qs, headers);
|
|
|
|
returnData.push({ success: true });
|
|
}
|
|
//https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
|
|
if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
if (returnAll) {
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', '', 'GET', '');
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', 0) as number;
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListAllMyBucketsResult.Buckets.Bucket', '', 'GET', '', '', qs);
|
|
responseData = responseData.slice(0, qs.limit);
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
|
|
//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) as boolean;
|
|
const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject;
|
|
|
|
if (additionalFields.prefix) {
|
|
qs['prefix'] = additionalFields.prefix as string;
|
|
}
|
|
|
|
if (additionalFields.encodingType) {
|
|
qs['encoding-type'] = additionalFields.encodingType as string;
|
|
}
|
|
|
|
if (additionalFields.delmiter) {
|
|
qs['delimiter'] = additionalFields.delmiter 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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._ as string;
|
|
|
|
if (returnAll) {
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
} else {
|
|
qs['max-keys'] = this.getNodeParameter('limit', 0) as number;
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
responseData = responseData.ListBucketResult.Contents;
|
|
}
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData);
|
|
} else {
|
|
returnData.push(responseData);
|
|
}
|
|
}
|
|
}
|
|
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) as IDataObject;
|
|
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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', path, '', qs, headers, {}, region);
|
|
returnData.push({ success: true });
|
|
}
|
|
//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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, '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 = { 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 s3ApiRequestSOAP.call(this, bucketName, 'POST', '/', data, { delete: '' }, headers, {}, region);
|
|
|
|
responseData = { deleted: responseData.DeleteResult.Deleted };
|
|
}
|
|
returnData.push(responseData);
|
|
}
|
|
//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) as boolean;
|
|
const options = this.getNodeParameter('options', 0) as IDataObject;
|
|
|
|
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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
if (returnAll) {
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', 0) as number;
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
}
|
|
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);
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
}
|
|
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) as IDataObject;
|
|
|
|
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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', destination, '', qs, headers, {}, region);
|
|
returnData.push(responseData.CopyObjectResult);
|
|
|
|
}
|
|
//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(), 'Downloding a whole directory is not yet supported, please provide a file key');
|
|
}
|
|
|
|
let region = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
region = region.LocationConstraint._;
|
|
|
|
const response = await s3ApiRequestREST.call(this, bucketName, 'GET', `/${fileKey}`, '', qs, {}, { encoding: null, resolveWithFullResponse: true }, region);
|
|
|
|
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) {
|
|
// 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) as string;
|
|
|
|
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) as IDataObject;
|
|
|
|
if (options.versionId) {
|
|
qs.versionId = options.versionId as string;
|
|
}
|
|
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'DELETE', `/${fileKey}`, '', qs, {}, {}, region);
|
|
|
|
returnData.push({ success: true });
|
|
}
|
|
//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) as boolean;
|
|
const options = this.getNodeParameter('options', 0) as IDataObject;
|
|
|
|
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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
if (returnAll) {
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', 0) as number;
|
|
responseData = await s3ApiRequestSOAPAllItems.call(this, 'ListBucketResult.Contents', bucketName, 'GET', '', '', qs, {}, {}, region);
|
|
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);
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
//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) as boolean;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
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 s3ApiRequestSOAP.call(this, bucketName, 'GET', '', '', { location: '' });
|
|
|
|
const region = responseData.LocationConstraint._;
|
|
|
|
if (isBinaryData) {
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string;
|
|
|
|
if (items[i].binary === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!');
|
|
}
|
|
|
|
if ((items[i].binary as IBinaryKeyData)[binaryPropertyName] === undefined) {
|
|
throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`);
|
|
}
|
|
|
|
const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName];
|
|
|
|
body = Buffer.from(binaryData.data, BINARY_ENCODING) as Buffer;
|
|
|
|
headers['Content-Type'] = binaryData.mimeType;
|
|
|
|
headers['Content-MD5'] = createHash('md5').update(body).digest('base64');
|
|
|
|
responseData = await s3ApiRequestSOAP.call(this, bucketName, 'PUT', `${path}${fileName || binaryData.fileName}`, body, qs, headers, {}, region);
|
|
|
|
} 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 s3ApiRequestSOAP.call(this, bucketName, 'PUT', `${path}${fileName}`, body, qs, headers, {}, region);
|
|
}
|
|
returnData.push({ success: true });
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
if (resource === 'file' && operation === 'download') {
|
|
items[i].json = { error: error.message };
|
|
}else {
|
|
returnData.push({ error: error.message });
|
|
}
|
|
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.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|
|
}
|