Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(AWS Deploy): Generalize
uploadZipFile
logic
- Loading branch information
Showing
4 changed files
with
71 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = (putParams, deploymentBucketOptions) => { | ||
const encryptionFields = [ | ||
['serverSideEncryption', 'ServerSideEncryption'], | ||
['sseCustomerAlgorithim', 'SSECustomerAlgorithm'], | ||
['sseCustomerKey', 'SSECustomerKey'], | ||
['sseCustomerKeyMD5', 'SSECustomerKeyMD5'], | ||
['sseKMSKeyId', 'SSEKMSKeyId'], | ||
]; | ||
|
||
const params = putParams; | ||
|
||
encryptionFields.forEach((element) => { | ||
if (deploymentBucketOptions[element[0]]) { | ||
params[element[1]] = deploymentBucketOptions[element[0]]; | ||
} | ||
}); | ||
|
||
return params; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const crypto = require('crypto'); | ||
const setS3UploadEncryptionOptions = require('../../../aws/set-s3-upload-encryption-options'); | ||
|
||
module.exports = { | ||
async uploadZipFile({ filename, s3KeyDirname }) { | ||
const fileName = filename.split(path.sep).pop(); | ||
|
||
// TODO refactor to be async (use util function to compute checksum async) | ||
const data = fs.readFileSync(filename); | ||
const fileHash = crypto.createHash('sha256').update(data).digest('base64'); | ||
|
||
const artifactStream = fs.createReadStream(filename); | ||
// As AWS SDK request might be postponed (requests are queued) | ||
// eventual stream error may crash the process (it's thrown as uncaught if not observed). | ||
// Below lines prevent that | ||
let streamError; | ||
artifactStream.on('error', (error) => (streamError = error)); | ||
|
||
let params = { | ||
Bucket: this.bucketName, | ||
Key: `${s3KeyDirname}/${fileName}`, | ||
Body: artifactStream, | ||
ContentType: 'application/zip', | ||
Metadata: { | ||
filesha256: fileHash, | ||
}, | ||
}; | ||
|
||
const deploymentBucketObject = this.serverless.service.provider.deploymentBucketObject; | ||
if (deploymentBucketObject) { | ||
params = setS3UploadEncryptionOptions(params, deploymentBucketObject); | ||
} | ||
|
||
const response = await this.provider.request('S3', 'upload', params); | ||
// Interestingly, if request handling was queued, and stream errored (before being consumed by | ||
// AWS SDK) then SDK call succeeds without actually uploading a file to S3 bucket. | ||
// Below line ensures that eventual stream error is communicated | ||
if (streamError) throw streamError; | ||
return response; | ||
}, | ||
}; |