Skip to content

Commit e4e47a0

Browse files
committed
refactor: migrate request pre-signing
1 parent 8947a9f commit e4e47a0

File tree

7 files changed

+37
-19
lines changed

7 files changed

+37
-19
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@
515515
"@aws-sdk/smithy-client": "<3.696.0",
516516
"@aws-sdk/protocol-http": "<3.696.0",
517517
"@aws-sdk/util-arn-parser": "<3.696.0",
518+
"@aws-sdk/s3-request-presigner": "<3.696.0",
518519
"@aws/mynah-ui": "^4.23.0",
519520
"@gerhobbelt/gitignore-parser": "^0.2.0-9",
520521
"@iarna/toml": "^2.2.5",

packages/core/src/awsService/s3/commands/presignedURL.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ export async function presignedURLCommand(node: S3FileNode): Promise<void> {
2222
bucketName: node.bucket.name,
2323
key: node.file.key,
2424
time: validTime * 60,
25-
operation: 'getObject',
2625
}
2726

28-
const url = await s3Client.getSignedUrl(request).catch((e) => {
27+
const url = await s3Client.getSignedUrlForObject(request).catch((e) => {
2928
throw ToolkitError.chain(
3029
e,
3130
'Error creating the presigned URL. Make sure you have access to the requested file.'

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import { once } from './utilities/functionUtils'
3838
import { isWeb } from './extensionGlobals'
3939

4040
export type AwsClientConstructor<C> = new (o: AwsClientOptions) => C
41+
export type AwsCommandConstructor<CommandInput extends object, Command extends AwsCommand> = new (
42+
o: CommandInput
43+
) => Command
4144

4245
// AWS-SDKv3 does not export generic types for clients so we need to build them as needed
4346
// https://github.com/aws/aws-sdk-js-v3/issues/5856#issuecomment-2096950979

packages/core/src/shared/clients/clientWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
import * as vscode from 'vscode'
66
import globals from '../extensionGlobals'
7-
import { AwsClient, AwsClientConstructor, AwsCommand } from '../awsClientBuilderV3'
7+
import { AwsClient, AwsClientConstructor, AwsCommand, AwsCommandConstructor } from '../awsClientBuilderV3'
88
import { PaginationConfiguration, Paginator } from '@aws-sdk/types'
99
import { AsyncCollection, toCollection } from '../utilities/asyncCollection'
1010

@@ -29,7 +29,7 @@ export abstract class ClientWrapper<C extends AwsClient> implements vscode.Dispo
2929
}
3030

3131
protected async makeRequest<CommandInput extends object, CommandOutput extends object, Command extends AwsCommand>(
32-
command: new (o: CommandInput) => Command,
32+
command: AwsCommandConstructor<CommandInput, Command>,
3333
commandOptions: CommandInput
3434
): Promise<CommandOutput> {
3535
return await this.getClient().send(new command(commandOptions))

packages/core/src/shared/clients/s3.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
PutObjectCommand,
3232
S3Client as S3ClientSDK,
3333
} from '@aws-sdk/client-s3'
34+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
3435
import { ClientWrapper } from './clientWrapper'
3536
import { ToolkitError } from '../errors'
3637

@@ -96,8 +97,6 @@ export interface SignedUrlRequest {
9697
readonly bucketName: string
9798
readonly key: string
9899
readonly time: number
99-
readonly operation?: string
100-
readonly body?: string
101100
}
102101

103102
export interface UploadFileRequest {
@@ -306,18 +305,14 @@ export class S3Client extends ClientWrapper<S3ClientSDK> {
306305
*
307306
* @returns the string of the link to the presigned URL
308307
*/
309-
public async getSignedUrl(request: SignedUrlRequest): Promise<string> {
310-
const time = request.time
311-
const operation = request.operation ? request.operation : 'getObject'
312-
const s3 = await this.createS3()
313-
314-
const url = await s3.getSignedUrlPromise(operation, {
315-
Bucket: request.bucketName,
316-
Key: request.key,
317-
Body: request.body,
318-
Expires: time,
319-
})
320-
return url
308+
public async getSignedUrlForObject(request: SignedUrlRequest): Promise<string> {
309+
return await getSignedUrl(
310+
this.getClient(),
311+
new GetObjectCommand({ Bucket: request.bucketName, Key: request.key }),
312+
{
313+
expiresIn: request.time,
314+
}
315+
)
321316
}
322317

323318
/**

packages/core/src/test/awsService/s3/commands/presignedURL.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('presignedURLCommand', function () {
3636

3737
it('calls S3 to get the URL', async function () {
3838
getTestWindow().onDidShowInputBox((input) => input.acceptValue('20'))
39-
s3.getSignedUrl = sinon.stub().resolves(testUrl)
39+
s3.getSignedUrlForObject = sinon.stub().resolves(testUrl)
4040

4141
await presignedURLCommand(node)
4242

0 commit comments

Comments
 (0)