file storage workspace id prefix (#6230)

closes https://github.com/twentyhq/twenty/issues/6155

just an idea, i guess this could work well, but im open for discussion

---------

Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
rostaklein
2024-08-01 18:07:22 +02:00
committed by GitHub
parent 5c92ab937e
commit a424c63476
26 changed files with 727 additions and 231 deletions

View File

@ -1,11 +1,13 @@
import { Readable } from 'stream';
import {
CopyObjectCommand,
CreateBucketCommandInput,
DeleteObjectCommand,
DeleteObjectsCommand,
GetObjectCommand,
HeadBucketCommandInput,
HeadObjectCommand,
ListObjectsV2Command,
NotFound,
PutObjectCommand,
@ -13,6 +15,11 @@ import {
S3ClientConfig,
} from '@aws-sdk/client-s3';
import {
FileStorageException,
FileStorageExceptionCode,
} from 'src/engine/integrations/file-storage/interfaces/file-storage-exception';
import { StorageDriver } from './interfaces/storage-driver.interface';
export interface S3DriverOptions extends S3ClientConfig {
@ -115,13 +122,69 @@ export class S3Driver implements StorageDriver {
Key: `${params.folderPath}/${params.filename}`,
Bucket: this.bucketName,
});
const file = await this.s3Client.send(command);
if (!file || !file.Body || !(file.Body instanceof Readable)) {
throw new Error('Unable to get file stream');
try {
const file = await this.s3Client.send(command);
if (!file || !file.Body || !(file.Body instanceof Readable)) {
throw new Error('Unable to get file stream');
}
return Readable.from(file.Body);
} catch (error) {
if (error.name === 'NoSuchKey') {
throw new FileStorageException(
'File not found',
FileStorageExceptionCode.FILE_NOT_FOUND,
);
}
throw error;
}
}
return Readable.from(file.Body);
async move(params: {
from: { folderPath: string; filename: string };
to: { folderPath: string; filename: string };
}): Promise<void> {
const fromKey = `${params.from.folderPath}/${params.from.filename}`;
const toKey = `${params.to.folderPath}/${params.to.filename}`;
try {
// Check if the source file exists
await this.s3Client.send(
new HeadObjectCommand({
Bucket: this.bucketName,
Key: fromKey,
}),
);
// Copy the object to the new location
await this.s3Client.send(
new CopyObjectCommand({
CopySource: `${this.bucketName}/${fromKey}`,
Bucket: this.bucketName,
Key: toKey,
}),
);
// Delete the original object
await this.s3Client.send(
new DeleteObjectCommand({
Bucket: this.bucketName,
Key: fromKey,
}),
);
} catch (error) {
if (error.name === 'NotFound') {
throw new FileStorageException(
'File not found',
FileStorageExceptionCode.FILE_NOT_FOUND,
);
}
// For other errors, throw the original error
throw error;
}
}
async checkBucketExists(args: HeadBucketCommandInput) {