UNPKG

n8n-nodes-alicloud-oss

Version:
147 lines 5.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AlicloudOss = void 0; const buffer_1 = require("buffer"); const ali_oss_1 = __importDefault(require("ali-oss")); class AlicloudOss { constructor() { this.description = { displayName: 'Alicloud OSS', name: 'alicloudOss', icon: 'file:./alicloud-oss.logo.svg', group: ['transform'], version: 1, description: 'Operate Alibaba Cloud OSS within n8n workflows (ali-oss SDK)', defaults: { name: 'Alicloud OSS', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'alicloudOssApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Upload', value: 'upload' }, { name: 'Download', value: 'download' }, { name: 'List Objects', value: 'list' }, { name: 'Delete', value: 'delete' }, ], default: 'upload', noDataExpression: true, }, { displayName: 'Object Key', name: 'objectKey', type: 'string', description: 'Full path of the OSS object, e.g. folder/file.txt', default: '', displayOptions: { show: { operation: ['upload', 'download', 'delete'], }, }, required: true, }, { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', description: 'Binary property name for upload / download', default: 'data', displayOptions: { show: { operation: ['upload', 'download'], }, }, required: true, }, { displayName: 'Prefix', name: 'prefix', type: 'string', description: 'Prefix filter when listing objects', default: '', displayOptions: { show: { operation: ['list'], }, }, }, ], }; } async execute() { var _a; const items = this.getInputData(); const returnData = []; const credentials = (await this.getCredentials('alicloudOssApi')); const client = new ali_oss_1.default({ region: credentials.region, accessKeyId: credentials.accessKeyId, accessKeySecret: credentials.accessKeySecret, bucket: credentials.bucket, endpoint: credentials.endpoint || undefined, }); for (let i = 0; i < items.length; i++) { const operation = this.getNodeParameter('operation', i); try { if (operation === 'upload') { const objectKey = this.getNodeParameter('objectKey', i); const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName); const putRes = await client.put(objectKey, buffer_1.Buffer.from(binaryData.data)); returnData.push({ json: { success: true, url: putRes.url, objectKey } }); } else if (operation === 'download') { const objectKey = this.getNodeParameter('objectKey', i); const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const getRes = await client.get(objectKey); returnData.push({ json: { objectKey }, binary: { [binaryPropertyName]: await this.helpers.prepareBinaryData(getRes.content, objectKey), }, }); } else if (operation === 'list') { const prefix = this.getNodeParameter('prefix', i, ''); const listRes = await client.list({ prefix, 'max-keys': 1000, }, {}); const objects = (_a = listRes.objects) !== null && _a !== void 0 ? _a : []; for (const obj of objects) { returnData.push({ json: obj }); } } else if (operation === 'delete') { const objectKey = this.getNodeParameter('objectKey', i); await client.delete(objectKey); returnData.push({ json: { success: true, objectKey } }); } } catch (error) { const errMsg = error.message; if (this.continueOnFail()) { returnData.push({ json: { error: errMsg } }); continue; } throw error; } } return [returnData]; } } exports.AlicloudOss = AlicloudOss; //# sourceMappingURL=AlicloudOss.node.js.map