Skip to content

Commit

Permalink
chore: migrate AWS SDK for JavaScript v2 APIs to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jul 8, 2024
1 parent b0f182e commit e6b2799
Show file tree
Hide file tree
Showing 3 changed files with 1,246 additions and 75 deletions.
30 changes: 24 additions & 6 deletions apps/meteor/app/file-upload/ufs/AmazonS3/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import stream from 'stream';

import type { IUpload } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import S3 from 'aws-sdk/clients/s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { GetObjectCommand, S3, S3ClientConfig } from '@aws-sdk/client-s3';
import { check } from 'meteor/check';
import type { OptionalId } from 'mongodb';
import _ from 'underscore';
Expand All @@ -29,6 +30,10 @@ 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 +57,17 @@ class AmazonS3Store extends UploadFS.Store {

const classOptions = options;

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

options.getPath =
options.getPath ||
Expand All @@ -75,12 +90,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 +138,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 +165,8 @@ class AmazonS3Store extends UploadFS.Store {
params.Range = `${options.start} - ${options.end}`;
}

return s3.getObject(params).createReadStream();
const response = await s3.getObject(params);
return response.Body;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,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 @@ -304,7 +306,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

0 comments on commit e6b2799

Please sign in to comment.