Skip to content

Commit

Permalink
Add proxy support to SendToS3BucketService (#358)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4086

While testing the data export service on the development environment, it was identified that the upload process was unsuccessful due to the lack of support for proxy settings.
This change adds support for proxy by checking if a proxy server is configured. If a server is configured, we add these settings to a new customConfig object. We pass this object to the S3 client.

During testing on the development environment, we were waiting 6 minutes for the S3 client to error. This was due to the default set timeout being 6 minutes long. This was far too long during testing as we had to wait until we had a readable error. This change sets the timeout to be a maximum of 10 seconds now. This config is always passed to the S3 client.
  • Loading branch information
Beckyrose200 authored Sep 1, 2023
1 parent 4c2306c commit a7f4a37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
33 changes: 27 additions & 6 deletions app/services/data/export/send-to-s3-bucket.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* @module SendToS3BucketService
*/

const { PutObjectCommand, S3Client } = require('@aws-sdk/client-s3')
const fsPromises = require('fs').promises
const { HttpsProxyAgent, HttpProxyAgent } = require('hpagent')
const { NodeHttpHandler } = require('@smithy/node-http-handler')
const path = require('path')
const { PutObjectCommand, S3Client } = require('@aws-sdk/client-s3')

const requestConfig = require('../../../../config/request.config.js')
const S3Config = require('../../../../config/s3.config.js')

/**
Expand All @@ -26,16 +29,34 @@ async function go (filePath) {
Body: fileContent
}

await _uploadToBucket(params)
await _uploadFileToS3Bucket(params)
}

/**
* Uploads a file to an Amazon S3 bucket using the given parameters
* Sets the configuration settings for the S3 client
*
* @param {Object} params The parameters to use when uploading the file
* If the environment has a proxy then we set that here. The default timeout is 6 minutes but we believe that is far too
* long to wait. So, we set 'connectionTimeout' to be 10 seconds.
*/
async function _uploadToBucket (params) {
const s3Client = new S3Client()
function _customConfig () {
return {
requestHandler: new NodeHttpHandler({
// This uses the ternary operator to give either an `http/httpsAgent` object or an empty object, and the spread
// operator to bring the result back into the top level of the `customConfig` object.
...(requestConfig.httpProxy
? {
httpsAgent: new HttpsProxyAgent({ proxy: requestConfig.httpProxy }),
httpAgent: new HttpProxyAgent({ proxy: requestConfig.httpProxy })
}
: {}),
connectionTimeout: 10000
})
}
}

async function _uploadFileToS3Bucket (params) {
const customConfig = _customConfig()
const s3Client = new S3Client(customConfig)
const command = new PutObjectCommand(params)

await s3Client.send(command)
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@hapi/hapi": "^21.3.2",
"@hapi/inert": "^7.1.0",
"@hapi/vision": "^7.0.2",
"@smithy/node-http-handler": "^2.0.4",
"bcryptjs": "^2.4.3",
"blipp": "^4.0.2",
"dotenv": "^16.3.1",
Expand Down

0 comments on commit a7f4a37

Please sign in to comment.