mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-26 16:07:18 +00:00
* 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
65 lines
2.2 KiB
TypeScript
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 });
|
|
}
|
|
}
|