Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions apps/meteor/app/file-upload/ufs/AmazonS3/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { Readable } from 'stream';
import stream from 'stream';

import { GetObjectCommand, S3 } from '@aws-sdk/client-s3';
import type { S3ClientConfig } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import type { IUpload } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import S3 from 'aws-sdk/clients/s3';
import type { SdkStream } from '@smithy/types';
import { check } from 'meteor/check';
import type { OptionalId } from 'mongodb';
import _ from 'underscore';
Expand All @@ -29,6 +33,11 @@ export type S3Options = StoreOptions & {
};

class AmazonS3Store extends UploadFS.Store {
private params: {
Bucket: string;
ACL: string;
};

protected getPath: (file: IUpload) => string;

constructor(options: S3Options) {
Expand All @@ -52,7 +61,23 @@ class AmazonS3Store extends UploadFS.Store {

const classOptions = options;

const s3 = new S3(options.connection);
const { accessKeyId, secretAccessKey } = classOptions.connection;

if (!accessKeyId || !secretAccessKey) {
throw new Error('AWS credentials missing');
}

const s3Config: S3ClientConfig = {
region: options.connection.region,
endpoint: options.connection.endpoint,
credentials: {
accessKeyId,
secretAccessKey,
},
forcePathStyle: options.connection.s3ForcePathStyle,
};
const s3 = new S3(s3Config);
this.params = options.connection.params;

options.getPath =
options.getPath ||
Expand All @@ -75,12 +100,14 @@ class AmazonS3Store extends UploadFS.Store {

this.getRedirectURL = async (file, forceDownload = false) => {
const params = {
...this.params,
Key: this.getPath(file),
Expires: classOptions.URLExpiryTimeSpan,
ResponseContentDisposition: `${forceDownload ? 'attachment' : 'inline'}; filename="${encodeURI(file.name || '')}"`,
};

return s3.getSignedUrlPromise('getObject', params);
return getSignedUrl(s3, new GetObjectCommand(params), {
expiresIn: classOptions.URLExpiryTimeSpan,
});
};

/**
Expand Down Expand Up @@ -121,7 +148,7 @@ class AmazonS3Store extends UploadFS.Store {
};

try {
return s3.deleteObject(params).promise();
return s3.deleteObject(params);
} catch (err: any) {
SystemLogger.error(err);
}
Expand All @@ -148,7 +175,12 @@ class AmazonS3Store extends UploadFS.Store {
params.Range = `${options.start} - ${options.end}`;
}

return s3.getObject(params).createReadStream();
const response = await s3.getObject(params);
if (!response.Body) {
throw new Error('S3 object not found');
}

return response.Body as SdkStream<Readable>;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@
"typescript": "~5.3.3"
},
"dependencies": {
"@aws-sdk/client-s3": "<=3.565.0",
"@aws-sdk/s3-request-presigner": "<=3.565.0",
"@babel/runtime": "~7.22.15",
"@bugsnag/js": "~7.20.2",
"@bugsnag/plugin-react": "~7.19.0",
Expand Down Expand Up @@ -303,7 +305,6 @@
"asterisk-manager": "^0.2.0",
"atlassian-crowd-patched": "^0.5.1",
"autolinker": "^3.15.0",
"aws-sdk": "^2.1363.0",
"bad-words": "^3.0.4",
"bcrypt": "^5.0.1",
"body-parser": "1.20.2",
Expand Down
Loading