Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading file to AWS S3 bucket #211

Merged
merged 9 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .labrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module.exports = {
'__extends', '__assign', '__rest', '__decorate', '__param', '__metadata', '__awaiter', '__generator',
'__exportStar', '__createBinding', '__values', '__read', '__spread', '__spreadArrays', '__spreadArray', '__await',
'__asyncGenerator', '__asyncDelegator', '__asyncValues', '__makeTemplateObject', '__importStar', '__importDefault',
'__classPrivateFieldGet', '__classPrivateFieldSet',
'__classPrivateFieldGet', '__classPrivateFieldSet', '__esDecorate', '__runInitializers', '__propKey',
'__setFunctionName', '__classPrivateFieldIn',
// We also ignore globals exposed by global-agent:
'GLOBAL_AGENT','ROARR',
'GLOBAL_AGENT', 'ROARR',
// GlobalNotifier is added by us a global in a server plugin. It's how we make logging available anywhere in the app
// whilst avoiding having to pass it around
'GlobalNotifier',
Expand Down
2 changes: 1 addition & 1 deletion app/services/db-export/compress-files.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function go (filePath) {
await _compressFile(filePath)
global.GlobalNotifier.omg(`${filePath} successfully compressed to gzip.`)

return true
return `${filePath}.gz`
}

async function _compressFile (filePath) {
Expand Down
63 changes: 63 additions & 0 deletions app/services/db-export/send-to-s3-bucket.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

/**
* Sends a file to our AWS S3 bucket
* @module SendToS3BucketService
*/

const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')
const fs = require('fs')
const S3Config = require('../../../config/s3.config')
const path = require('path')
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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) {
if (!filePath) {
global.GlobalNotifier.omfg('ERROR uploading file to S3 bucket. No file path given')
return false
}
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved

const bucketName = S3Config.s3.bucket
const folderName = 'export'
const fileName = path.basename(filePath)
const fileContent = fs.readFileSync(filePath)

const params = {
Bucket: bucketName,
Key: `${folderName}/${fileName}`,
Body: fileContent
}
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved

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
}
18 changes: 18 additions & 0 deletions config/s3.config.js
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
Loading