Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate AWS SDK for JavaScript v2 APIs to v3 #32555

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
32 changes: 26 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,10 @@
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 { check } from 'meteor/check';
import type { OptionalId } from 'mongodb';
import _ from 'underscore';
Expand All @@ -29,6 +31,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 +59,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 +92,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 +140,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 +167,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
Loading