ToolJet/plugins/packages/s3/lib/operations.ts
Midhun G S f68c8e1885
[Vulnerability] package upgrades (#14861)
* chore: update nestjs-otel to version 8.0.1 and add systeminformation override

* remove unused API metrics configuration from OpenTelemetry setup

* fixed ldap flaky case

* Update: Sendgrid package to 8.1.6 (#14833)

* updated sendgrid

* fix: update listBuckets function to use default options for ListBucketsCommand

---------

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>

* Vulnerability Issues (#14834)

* Security Fixes

* set version

* utl harper db

* s3

* marketplace package-lock

* Remove package-lock.json files

* chore: update lerna to version 9.0.3 in package.json

* Implement code changes to enhance functionality and improve performance

* removing options from s3 bucket in markeplace

* supabase build type fixes

---------

Co-authored-by: Adish M <adish.madhu@gmail.com>
Co-authored-by: Yukti Goyal <yuktigoyal02@gmail.com>
Co-authored-by: Adish M <44204658+adishM98@users.noreply.github.com>
Co-authored-by: abhijeet760 <abhijeet@tooljet.com>
Co-authored-by: Rudhra Deep Biswas <98055396+rudeUltra@users.noreply.github.com>
Co-authored-by: Rudhra Deep Biswas <rudra21ultra@gmail.com>
2026-01-08 17:46:51 +05:30

105 lines
3.4 KiB
TypeScript

import {
GetObjectCommand,
ListBucketsCommand,
PutObjectCommand,
DeleteObjectCommand,
S3Client,
CreateBucketCommand,
ListObjectsV2Command,
} from '@aws-sdk/client-s3';
// https://aws.amazon.com/blogs/developer/generate-presigned-url-modular-aws-sdk-javascript/
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { QueryOptions } from './types';
export async function listBuckets(client: S3Client, options: QueryOptions): Promise<object> {
const command = new ListBucketsCommand();
return client.send(command);
}
export async function listObjects(client: S3Client, options: object): Promise<object> {
const command = new ListObjectsV2Command({
Bucket: options['bucket'],
Prefix: options['prefix'],
MaxKeys: options['maxKeys'],
StartAfter: options['offset'],
ContinuationToken: options['continuationToken'],
});
return client.send(command);
}
export async function signedUrlForGet(client: S3Client, options: QueryOptions): Promise<object> {
const command = new GetObjectCommand({
Bucket: options.bucket,
Key: options.key,
});
const url = await getSignedUrl(client, command, {
expiresIn: options.expiresIn || 3600,
});
return { url };
}
export async function createBucket(client: S3Client, options: QueryOptions): Promise<object> {
const createBucketCommand = new CreateBucketCommand({
Bucket: options.bucket,
});
return await client.send(createBucketCommand);
}
export async function getObject(client: S3Client, options: QueryOptions): Promise<object> {
// Create a helper function to convert a ReadableStream to a string.
const streamToString = (stream) =>
new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
});
// Get the object from the Amazon S3 bucket. It is returned as a ReadableStream.
const command = new GetObjectCommand({
Bucket: options.bucket,
Key: options.key,
});
const data = await client.send(command);
// Convert the ReadableStream to a string.
const bodyContents = await streamToString(data.Body);
return { ...data, Body: bodyContents };
}
export async function uploadObject(client: S3Client, options: QueryOptions): Promise<object> {
const encoding = options.encoding || 'utf8';
const uploadData = (data: any, contentType: string) => {
if (!data) {
return;
}
return typeof data === 'object' && contentType.includes('application/json') ? JSON.stringify(data) : data;
};
const body = new Buffer(uploadData(options.data, options.contentType), encoding);
const command = new PutObjectCommand({
Bucket: options.bucket,
Key: options.key,
Body: body,
ContentType: options.contentType,
ContentEncoding: encoding,
});
return await client.send(command);
}
export async function signedUrlForPut(client: S3Client, options: QueryOptions): Promise<object> {
const command = new PutObjectCommand({
Bucket: options.bucket,
Key: options.key,
ContentType: options.contentType,
});
const url = await getSignedUrl(client, command, {
expiresIn: options.expiresIn || 3600,
});
return { url };
}
export async function removeObject(client: S3Client, options: QueryOptions): Promise<object> {
const command = new DeleteObjectCommand({
Bucket: options.bucket,
Key: options.key,
});
return await client.send(command);
}