|
2 | 2 |
|
3 | 3 | const process = require('process') |
4 | 4 | const crypto = require('crypto') |
| 5 | + |
| 6 | +const { |
| 7 | + S3Client, |
| 8 | + CreateBucketCommand, |
| 9 | + PutObjectCommand |
| 10 | +} = require('@aws-sdk/client-s3') |
| 11 | + |
5 | 12 | // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property |
6 | 13 | const S3_LOCATION_POSSIBLE_VALUES = [ |
7 | 14 | 'EU', |
@@ -33,12 +40,8 @@ const S3_LOCATION_POSSIBLE_VALUES = [ |
33 | 40 | ] |
34 | 41 |
|
35 | 42 | 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) |
42 | 45 | } |
43 | 46 |
|
44 | 47 | _md5 (str) { |
@@ -107,30 +110,21 @@ class S3Deploy { |
107 | 110 | LocationConstraint: s3Location |
108 | 111 | } |
109 | 112 | } |
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 | + } |
120 | 120 | } |
121 | 121 |
|
122 | 122 | _putObject (params, buffer) { |
123 | | - const _params = { |
| 123 | + return this.client.send(new PutObjectCommand({ |
124 | 124 | Body: buffer, |
125 | 125 | Bucket: params.bucketName, |
126 | 126 | 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 | + })) |
134 | 128 | } |
135 | 129 |
|
136 | 130 | putPackage (params, region, buffer) { |
|
0 commit comments