Skip to content

Commit 84b3be6

Browse files
authored
Merge pull request motdotla#790 from abetomo/migration-to-aws-sdk-v3-s3
sdk-v3: Migrate lib/s3_deploy to V3
2 parents 8082566 + 4a54958 commit 84b3be6

File tree

7 files changed

+11339
-6399
lines changed

7 files changed

+11339
-6399
lines changed

lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,20 +958,20 @@ they may not work as expected in the Lambda environment.
958958
async _deployToRegion (program, params, region, buffer) {
959959
// sdk v3 todo: Migration of aws.updateConfig.
960960
aws.updateConfig(program, region)
961+
const config = { region }
961962

962963
console.log('=> Reading event source file to memory')
963964
const eventSourceList = this._eventSourceList(program)
964965

965966
if (this._isUseS3(program)) {
966-
const s3Deploy = new S3Deploy(aws.sdk, region)
967+
const s3Deploy = new S3Deploy(config)
967968
params.Code = await s3Deploy.putPackage(params, region, buffer)
968969
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
969970
} else {
970971
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
971972
}
972973
console.log(params)
973974

974-
const config = { region }
975975
const lambdaClient = new LambdaClient(config)
976976
// Migrating to v3.
977977
const scheduleEvents = new ScheduleEvents(aws.sdk, region)

lib/s3_deploy.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
const process = require('process')
44
const crypto = require('crypto')
5+
6+
const {
7+
S3Client,
8+
CreateBucketCommand,
9+
PutObjectCommand
10+
} = require('@aws-sdk/client-s3')
11+
512
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property
613
const S3_LOCATION_POSSIBLE_VALUES = [
714
'EU',
@@ -33,12 +40,8 @@ const S3_LOCATION_POSSIBLE_VALUES = [
3340
]
3441

3542
class S3Deploy {
36-
constructor (aws, region) {
37-
// Authenticated `aws` object in `lib/main.js`
38-
this.s3 = new aws.S3({
39-
region,
40-
apiVersion: '2006-03-01'
41-
})
43+
constructor (config) {
44+
this.client = new S3Client(config)
4245
}
4346

4447
_md5 (str) {
@@ -107,30 +110,21 @@ class S3Deploy {
107110
LocationConstraint: s3Location
108111
}
109112
}
110-
return new Promise((resolve, reject) => {
111-
this.s3.createBucket(_params, (err, data) => {
112-
if (err) {
113-
// Ignored created
114-
if (err.code === 'BucketAlreadyOwnedByYou') return resolve({})
115-
return reject(err)
116-
}
117-
resolve(data)
118-
})
119-
})
113+
try {
114+
return this.client.send(new CreateBucketCommand(_params))
115+
} catch (err) {
116+
// Ignored created
117+
if (err.code === 'BucketAlreadyOwnedByYou') return {}
118+
throw err
119+
}
120120
}
121121

122122
_putObject (params, buffer) {
123-
const _params = {
123+
return this.client.send(new PutObjectCommand({
124124
Body: buffer,
125125
Bucket: params.bucketName,
126126
Key: params.s3Key
127-
}
128-
return new Promise((resolve, reject) => {
129-
this.s3.putObject(_params, (err, data) => {
130-
if (err) reject(err)
131-
resolve(data)
132-
})
133-
})
127+
}))
134128
}
135129

136130
putPackage (params, region, buffer) {

0 commit comments

Comments
 (0)