Repository: motdotla/node-lambda
Pull request #791 (2025-04-19 07:38:39)

sdk-v3: Migrate lib/s3_events to V3
GH-641


Patch:
diff --git a/lib/main.js b/lib/main.js
index 00586ec..205ac9b 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -975,7 +975,7 @@ they may not work as expected in the Lambda environment.
     const lambdaClient = new LambdaClient(config)
     // Migrating to v3.
     const scheduleEvents = new ScheduleEvents(aws.sdk, region)
-    const s3Events = new S3Events(aws.sdk, region)
+    const s3Events = new S3Events(config)
     const cloudWatchLogs = new CloudWatchLogs(config)
 
     const existsFunction = await (async () => {
diff --git a/lib/s3_events.js b/lib/s3_events.js
index a083dab..f7bc566 100644
--- a/lib/s3_events.js
+++ b/lib/s3_events.js
@@ -1,20 +1,16 @@
 'use strict'
 
+const { S3Client, PutBucketNotificationConfigurationCommand } = require('@aws-sdk/client-s3')
+const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
+
 /**
  * Do not create S3 bucket.
  * Put the Notification Configuration in the existing Bucket.
  */
 class S3Events {
-  constructor (aws, region) {
-    // Authenticated `aws` object in `lib/main.js`
-    this.lambda = new aws.Lambda({
-      region,
-      apiVersion: '2015-03-31'
-    })
-    this.s3 = new aws.S3({
-      region,
-      apiVersion: '2006-03-01'
-    })
+  constructor (config) {
+    this.s3Client = new S3Client(config)
+    this.lambdaClient = new LambdaClient(config)
   }
 
   _functionName (params) {
@@ -36,17 +32,14 @@ class S3Events {
   }
 
   _addPermission (params) {
-    return new Promise((resolve, reject) => {
-      const _params = this._addPermissionParams(params)
-      this.lambda.addPermission(_params, (err, data) => {
-        if (err) {
-          if (err.code !== 'ResourceConflictException') reject(err)
-          // If it exists it will result in an error but there is no problem.
-          resolve('Permission already set')
-        }
-        resolve(data)
-      })
-    })
+    const _params = this._addPermissionParams(params)
+    try {
+      return this.lambdaClient.send(new AddPermissionCommand(_params))
+    } catch (err) {
+      if (err.code !== 'ResourceConflictException') throw err
+      // If it exists it will result in an error but there is no problem.
+      return 'Permission already set'
+    }
   }
 
   _lambdaFunctionConfiguration (params) {
@@ -86,12 +79,8 @@ class S3Events {
   }
 
   _putBucketNotificationConfiguration (putBucketNotificationConfigurationParams) {
-    return new Promise((resolve, reject) => {
-      this.s3.putBucketNotificationConfiguration(putBucketNotificationConfigurationParams, (err, data) => {
-        if (err) reject(err)
-        resolve(data)
-      })
-    })
+    return this.s3Client.send(
+      new PutBucketNotificationConfigurationCommand(putBucketNotificationConfigurationParams))
   }
 
   add (paramsList) {


Test patch:
diff --git a/test/main.js b/test/main.js
index 6c45df6..28533f7 100644
--- a/test/main.js
+++ b/test/main.js
@@ -20,6 +20,7 @@ const { mockClient } = require('aws-sdk-client-mock')
 
 const {
   LambdaClient,
+  AddPermissionCommand,
   CreateEventSourceMappingCommand,
   CreateFunctionCommand,
   DeleteEventSourceMappingCommand,
@@ -45,6 +46,7 @@ const mockCloudWatchLogsClient = mockClient(CloudWatchLogsClient)
 const {
   S3Client,
   CreateBucketCommand,
+  PutBucketNotificationConfigurationCommand,
   PutObjectCommand
 } = require('@aws-sdk/client-s3')
 const mockS3Client = mockClient(S3Client)
@@ -142,9 +144,6 @@ const _mockSetting = () => {
   awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
     callback(null, {})
   })
-  awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
-    callback(null, {})
-  })
 
   Object.keys(lambdaMockSettings).forEach((method) => {
     awsMock.mock('Lambda', method, (params, callback) => {
@@ -181,6 +180,7 @@ describe('lib/main', function () {
 
     // for sdk v3
     mockLambdaClient.reset()
+    mockLambdaClient.on(AddPermissionCommand).resolves(lambdaMockSettings.addPermission)
     mockLambdaClient.on(CreateEventSourceMappingCommand).resolves(lambdaMockSettings.createEventSourceMapping)
     mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction)
     mockLambdaClient.on(DeleteEventSourceMappingCommand).resolves(lambdaMockSettings.deleteEventSourceMapping)
@@ -199,6 +199,7 @@ describe('lib/main', function () {
 
     mockS3Client.reset()
     mockS3Client.on(CreateBucketCommand).resolves({})
+    mockS3Client.on(PutBucketNotificationConfigurationCommand).resolves({})
     mockS3Client.on(PutObjectCommand).resolves({})
   })
   after(() => {
diff --git a/test/s3_events.js b/test/s3_events.js
index aa003af..ea9b4ef 100644
--- a/test/s3_events.js
+++ b/test/s3_events.js
@@ -4,9 +4,11 @@ let assert
 import('chai').then(chai => {
   assert = chai.assert
 })
-const path = require('path')
-const aws = require('aws-sdk-mock')
-aws.setSDK(path.resolve('node_modules/aws-sdk'))
+const { S3Client, PutBucketNotificationConfigurationCommand } = require('@aws-sdk/client-s3')
+const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
+const { mockClient } = require('aws-sdk-client-mock')
+const mockS3Client = mockClient(S3Client)
+const mockLambdaClient = mockClient(LambdaClient)
 const S3Events = require('../lib/s3_events')
 
 const params = {
@@ -33,22 +35,15 @@ const mockResponse = {
 
 let s3Events = null
 
-/* global before, after, describe, it */
+/* global before, describe, it */
 describe('lib/s3_events', () => {
   before(() => {
-    aws.mock('Lambda', 'addPermission', (params, callback) => {
-      callback(null, mockResponse.addPermission)
-    })
-    aws.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
-      callback(null, mockResponse.putBucketNotificationConfiguration)
-    })
-
-    s3Events = new S3Events(require('aws-sdk'))
-  })
+    mockS3Client.reset()
+    mockS3Client.on(PutBucketNotificationConfigurationCommand).resolves(mockResponse.putBucketNotificationConfiguration)
+    mockLambdaClient.reset()
+    mockLambdaClient.on(AddPermissionCommand).resolves(mockResponse.addPermission)
 
-  after(() => {
-    aws.restore('Lambda')
-    aws.restore('S3')
+    s3Events = new S3Events({ region: 'us-west-1' })
   })
 
   describe('_functionName', () => {