Skip to content

Commit

Permalink
Optional dependencies and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Nov 14, 2024
1 parent a9e7858 commit 20bd27b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
95 changes: 47 additions & 48 deletions mod/sign/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Signs requests to S3. Provides functions for get, list, delete and put to S3.

//The S3 packages are optional.
//Need a temporary assignment to determine if they are available.
let
let
S3Client,
PutObjectCommand,
GetObjectCommand,
Expand All @@ -16,33 +16,32 @@ let
getSignedUrl;

//Check for credentials
if(!process.env?.AWS_S3_CLIENT){
if (!process.env?.AWS_S3_CLIENT) {
console.log('S3 Sign: Missing credentials from env: AWS_S3_CLIENT')
module.exports = null
}
else{
}
else {

//Attempt import if credentials are found
try{
try {

//Assign constructors and functions from the sdks.
const clientSDK = require('@aws-sdk/client-s3');

getSignedUrl = require('@aws-sdk/s3-request-presigner').getSignedUrl;

S3Client = clientSDK.S3Client
PutObjectCommand = clientSDK.PutObjectCommand
GetObjectCommand = clientSDK.GetObjectCommand
DeleteObjectCommand = clientSDK.DeleteObjectCommand
ListObjectsCommand = clientSDK.ListObjectsCommand

getSignedUrl = require('@aws-sdk/s3-request-presigner').getSignedUrl;


//Export the function .
module.exports = s3
}
catch(err){
catch (err) {

//The module has not been installed.
if(err.code === 'MODULE_NOT_FOUND'){
if (err.code === 'MODULE_NOT_FOUND') {
console.log('AWS-SDK is not available')
module.exports = null
}
Expand Down Expand Up @@ -70,7 +69,7 @@ Provides methods for list, get, trash and put
@returns {Promise<String>} The signed url associated to the request params.
**/
async function s3(req, res){
async function s3(req, res) {

//Read credentials from an env key
const credentials = Object.fromEntries(new URLSearchParams(process.env.AWS_S3_CLIENT))
Expand All @@ -84,10 +83,10 @@ async function s3(req, res){

//Assign the corresponding function to the requested command
const commands = {
get: () => objectAction(req.params,GetObjectCommand),
trash: () => objectAction(req.params,DeleteObjectCommand),
put: () => objectAction(req.params,PutObjectCommand),
list: () => objectAction(req.params,ListObjectsCommand)
get: () => objectAction(req.params, GetObjectCommand),
trash: () => objectAction(req.params, DeleteObjectCommand),
put: () => objectAction(req.params, PutObjectCommand),
list: () => objectAction(req.params, ListObjectsCommand)
}

if (!Object.hasOwn(commands, req.params.command)) {
Expand Down Expand Up @@ -115,36 +114,36 @@ Generates the signed url for the command and parameters specified from the reque
**/
async function objectAction(reqParams, objectCommand) {

//The parameters required per action for S3
//S3 Parameters are capitalised
const actionParams = {
get: {'key':'Key','bucket': 'Bucket'},
list: {'bucket': 'Bucket'},
put: {'key': 'Key','bucket': 'Bucket','region': 'Region'},
trash: {'key': 'Key','bucket': 'Bucket'}
}
//The parameters required per action for S3
//S3 Parameters are capitalised
const actionParams = {
get: { 'key': 'Key', 'bucket': 'Bucket' },
list: { 'bucket': 'Bucket' },
put: { 'key': 'Key', 'bucket': 'Bucket', 'region': 'Region' },
trash: { 'key': 'Key', 'bucket': 'Bucket' }
}

try {
//Transfrom our keys into aws key names
const commandParams = Object.keys(reqParams)
.filter(key => Object.keys(actionParams[reqParams.command]).includes(key))
.reduce((acc, key) => ({
...acc,
...{ [actionParams[reqParams.command][key]]: reqParams[key]}
}),
{}
)

const command = new objectCommand(commandParams)
const signedURL = await getSignedUrl(reqParams.s3Client, command, {
expiresIn: 3600,
});
return JSON.stringify(signedURL);
} catch (err) {
console.error(err)
}
try {

//Transfrom our keys into aws key names
const commandParams = Object.keys(reqParams)
.filter(key => Object.keys(actionParams[reqParams.command]).includes(key))
.reduce((acc, key) => ({
...acc,
...{ [actionParams[reqParams.command][key]]: reqParams[key] }
}),
{}
)

const command = new objectCommand(commandParams)

const signedURL = await getSignedUrl(reqParams.s3Client, command, {
expiresIn: 3600,
});

return JSON.stringify(signedURL);

} catch (err) {
console.error(err)
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"test-docs": "jsdoc --configure jsdoc_test.json --verbose"
},
"license": "MIT",
"dependencies": {
"optionalDependencies": {
"@aws-sdk/client-s3": "^3.621.0",
"@aws-sdk/s3-request-presigner": "^3.621.0"
},
"dependencies": {
"@aws-sdk/cloudfront-signer": "^3.621.0",
"@aws-sdk/s3-request-presigner": "^3.621.0",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.7",
"pg": "^8.7.3",
Expand Down

0 comments on commit 20bd27b

Please sign in to comment.