-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uploading file to AWS S3 bucket (#211)
https://eaflood.atlassian.net/browse/WATER-3966 This pull request is just a small part of a larger project that involves exporting all our database schemas, converting them into CSV files, and uploading them to our Amazon S3 bucket. This PR's primary focus is connecting to the amazon s3 bucket and uploading the file. To expedite the export process and see the output sooner, we are using a vertical slicing approach, rather than a horizontal one, which means exporting a single table at a time from each schema.
- Loading branch information
1 parent
d749ffe
commit 1225599
Showing
8 changed files
with
7,044 additions
and
4,428 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
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,57 @@ | ||
'use strict' | ||
|
||
/** | ||
* Sends a file to our AWS S3 bucket | ||
* @module SendToS3BucketService | ||
*/ | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const { PutObjectCommand, S3Client } = require('@aws-sdk/client-s3') | ||
|
||
const S3Config = require('../../../config/s3.config') | ||
/** | ||
* Sends a file to our AWS S3 Bucket using the filePath that it receives | ||
* | ||
* @param {String} filePath A string containing the path of the file to send to the S3 bucket | ||
* | ||
* @returns {Boolean} True if the file is uploaded successfully and false if not | ||
*/ | ||
async function go (filePath) { | ||
const bucketName = S3Config.s3.bucket | ||
const fileName = path.basename(filePath) | ||
const fileContent = fs.readFileSync(filePath) | ||
|
||
const params = { | ||
Bucket: bucketName, | ||
Key: `export/${fileName}`, | ||
Body: fileContent | ||
} | ||
|
||
return _uploadToBucket(params, fileName) | ||
} | ||
/** | ||
* Uploads a file to an Amazon S3 bucket using the given parameters | ||
* | ||
* @param {Object} params The parameters to use when uploading the file. | ||
* @param {String} fileName The name of the file to upload | ||
* | ||
* @returns {Boolean} True if the file is uploaded successfully and false if not | ||
*/ | ||
async function _uploadToBucket (params, fileName) { | ||
const s3Client = new S3Client() | ||
const command = new PutObjectCommand(params) | ||
|
||
try { | ||
await s3Client.send(command) | ||
global.GlobalNotifier.omg(`The file ${fileName} was uploaded successfully`) | ||
return true | ||
} catch (error) { | ||
global.GlobalNotifier.omfg(`ERROR uploading file: ${error.message}`) | ||
return false | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
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,18 @@ | ||
'use strict' | ||
|
||
/** | ||
* Config values used for the amazon s3 bucket | ||
* @module S3Config | ||
*/ | ||
|
||
// We require dotenv directly in each config file to support unit tests that depend on this this subset of config. | ||
// Requiring dotenv in multiple places has no effect on the app when running for real. | ||
require('dotenv').config() | ||
|
||
const config = { | ||
s3: { | ||
bucket: process.env.AWS_MAINTENANCE_BUCKET | ||
} | ||
} | ||
|
||
module.exports = config |
Oops, something went wrong.