Skip to content

Commit 09fe9d1

Browse files
Merge pull request #80 from oss-serverless/v3-sdk-fixes
Fixes for the v3 SDK
2 parents 139880d + c10deda commit 09fe9d1

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

lib/aws/client-factory.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { LambdaClient } = require('@aws-sdk/client-lambda');
1313
const { S3Client } = require('@aws-sdk/client-s3');
1414
const { SSMClient } = require('@aws-sdk/client-ssm');
1515
const { STSClient } = require('@aws-sdk/client-sts');
16+
const { Upload } = require('@aws-sdk/lib-storage');
1617

1718
// Map service names to their client classes
1819
const CLIENT_MAP = {
@@ -63,12 +64,22 @@ class AWSClientFactory {
6364
/**
6465
* Send a command to an AWS service
6566
* @param {string} serviceName - Name of the AWS service
66-
* @param {Object} command - AWS SDK v3 command instance
67+
* @param {Object} command - AWS SDK v3 command instance or special upload indicator
6768
* @param {Object} clientConfig - Optional client configuration override
6869
* @returns {Promise} Result of the AWS API call
6970
*/
7071
async send(serviceName, command, clientConfig = {}) {
7172
const client = this.getClient(serviceName, clientConfig);
73+
74+
if (serviceName === 'S3' && command._isUploadRequest) {
75+
const upload = new Upload({
76+
client,
77+
params: command.params,
78+
});
79+
80+
return upload.done();
81+
}
82+
7283
return client.send(command);
7384
}
7485
}

lib/aws/commands.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,13 @@ const COMMAND_MAP = {
230230
*/
231231
function getCommand(serviceName, methodName) {
232232
const serviceCommands = COMMAND_MAP[serviceName];
233+
233234
if (!serviceCommands) {
234235
throw new Error(`Unknown AWS service: ${serviceName}`);
235236
}
236237

237238
const CommandClass = serviceCommands[methodName];
239+
238240
if (!CommandClass) {
239241
throw new Error(`Unknown method '${methodName}' for service '${serviceName}'`);
240242
}
@@ -250,7 +252,15 @@ function getCommand(serviceName, methodName) {
250252
* @returns {Object} Command instance
251253
*/
252254
function createCommand(serviceName, methodName, params = {}) {
255+
if (serviceName === 'S3' && methodName === 'upload') {
256+
return {
257+
_isUploadRequest: true,
258+
params,
259+
};
260+
}
261+
253262
const CommandClass = getCommand(serviceName, methodName);
263+
254264
return new CommandClass(params);
255265
}
256266

lib/plugins/aws/invoke.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,21 @@ class AwsInvoke {
100100
return this.provider.request('Lambda', 'invoke', params);
101101
}
102102

103+
payloadToString(payload) {
104+
if (payload instanceof Uint8Array) {
105+
return new TextDecoder().decode(payload);
106+
}
107+
108+
if (Buffer.isBuffer(payload)) {
109+
return payload.toString();
110+
}
111+
112+
return payload;
113+
}
114+
103115
log(invocationReply) {
104116
if (invocationReply.Payload) {
105-
const response = JSON.parse(invocationReply.Payload);
117+
const response = JSON.parse(this.payloadToString(invocationReply.Payload));
106118

107119
writeText(JSON.stringify(response, null, 4));
108120
}

lib/plugins/aws/provider.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,10 +1798,20 @@ class AwsProvider {
17981798
* @private
17991799
*/
18001800
_getV3BaseConfig() {
1801-
const credentials = this.getCredentials();
1801+
// Convert v2 credentials format to v3 format
1802+
const { credentials: v2Creds } = this.getCredentials();
1803+
const credentials =
1804+
v2Creds && v2Creds.accessKeyId
1805+
? {
1806+
accessKeyId: v2Creds.accessKeyId,
1807+
secretAccessKey: v2Creds.secretAccessKey,
1808+
sessionToken: v2Creds.sessionToken,
1809+
}
1810+
: undefined;
1811+
18021812
return buildClientConfig({
18031813
region: this.getRegion(),
1804-
credentials: credentials.accessKeyId ? credentials : undefined,
1814+
credentials,
18051815
});
18061816
}
18071817

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@aws-sdk/client-ssm": "^3.588.0",
4242
"@aws-sdk/client-sts": "^3.588.0",
4343
"@aws-sdk/lib-dynamodb": "^3.588.0",
44+
"@aws-sdk/lib-storage": "^3.588.0",
4445
"@aws-sdk/credential-providers": "^3.588.0",
4546
"@serverless/utils": "^6.13.1",
4647
"ajv": "^8.12.0",

0 commit comments

Comments
 (0)