ToolJet/server/plugins/datasources/s3/index.ts
Akshay 44f3b9d8d0
Feature: AWS S3 Integration 🚀 (#1333)
* add ability to list buckets/objects and create signed url fot GET and PUT

* ask to specify region when adding s3 buckets

* add ability to read file contents from s3

* add ability to upload to s3 bucket

* rename s3 dropdown options

* update region dropdown for s3
2021-11-02 11:42:46 +05:30

65 lines
2.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { ConnectionTestResult } from 'src/modules/data_sources/connection_test_result.type';
import { QueryResult } from 'src/modules/data_sources/query_result.type';
import { QueryService } from 'src/modules/data_sources/query_service.interface';
import { getObject, uploadObject, listBuckets, listObjects, signedUrlForGet, signedUrlForPut } from './operations';
import { S3Client } from '@aws-sdk/client-s3';
import { QueryError } from 'src/modules/data_sources/query.error';
@Injectable()
export default class S3QueryService implements QueryService {
async run(sourceOptions: any, queryOptions: any, dataSourceId: string): Promise<QueryResult> {
const operation = queryOptions.operation;
const client = await this.getConnection(sourceOptions, { operation });
let result = {};
try {
switch (operation) {
case 'list_buckets':
result = await listBuckets(client, {});
break;
case 'list_objects':
result = await listObjects(client, queryOptions);
break;
case 'get_object':
result = await getObject(client, queryOptions);
break;
case 'upload_object':
result = await uploadObject(client, queryOptions);
break;
case 'signed_url_for_get':
result = await signedUrlForGet(client, queryOptions);
break;
case 'signed_url_for_put':
result = await signedUrlForPut(client, queryOptions);
break;
}
} catch (error) {
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: object): Promise<ConnectionTestResult> {
const client: S3Client = await this.getConnection(sourceOptions, {
operation: 'list_objects',
});
await listBuckets(client, {});
return {
status: 'ok',
};
}
async getConnection(sourceOptions: any, options?: object): Promise<any> {
const credentials = {
accessKeyId: sourceOptions['access_key'],
secretAccessKey: sourceOptions['secret_key'],
};
return new S3Client({ region: sourceOptions['region'], credentials });
}
}