Forked from chris-burkhardt/NodeJS-base64encodedimage-to-S3.txt
Created
October 27, 2021 03:39
-
-
Save pritam3010/2f119ac1cabb26fe73dcf8d93e8d7eb5 to your computer and use it in GitHub Desktop.
Revisions
-
chris-burkhardt revised this gist
Sep 14, 2021 . 1 changed file with 39 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,28 +19,30 @@ module.exports = async function (inputs, context) { } /** * Uploads a Base64 encoded string into an S3 bucket after validating its MIME type * * @param {string} imageData Base64 encoded image string * @param {string} imageName Name of image for S3 bucket key * @param {object} inputs Request object * @param {object} context * * @returns The S3 bucker folder location as a string (does not include URL) */ async function uploadImageToS3(imageData, imageName, inputs, context) { const imageMimeType = await validateImageMIMEType(imageData, inputs, context); const body = Buffer.from(imageData.split('base64')[1], 'base64'); const ts = new Date().getTime(); // timestamp labeled image name const s3ImageLocation = `folderName/${imageName.toLowerCase()}_${ts}.jpeg` const params = { Bucket: 's3-bucket-name-here', Key: s3ImageLocation, Body: Buffer.from(body, 'base64'), ContentEncoding: 'base64', ContentType: imageMimeType }; try { @@ -53,6 +55,39 @@ async function uploadImageToS3(imageData, imageName, inputs, context) { } } /** * Extracts the MIME type from the image in the request and throws error if its not supported type * * @param {string} imageData base64 encoded image data from request * @param {object} inputs * @param {object} context */ async function validateImageMIMEType(imageData, inputs, context) { const imageDecode = /data:(.*);base64,(.*)/.exec(imageData); const supportedMimeTypes = [ 'image/jpeg', 'image/png', 'image/gif', 'image/bmp' ]; // imageDecode is null if something other than a base64 encoded image is sent in the request (string, int, etc.) const imageMimeType = imageDecode ? imageDecode[1] : null; const isSupportedMimeType = supportedMimeTypes.includes(imageMimeType); if (!imageMimeType || !isSupportedMimeType) { const errMessage = 'Upload not allowed due to invalid MIME type'; throwError(errMessage, inputs, context); const error = new Error(errMessage); error.status = 400; error.code = 'ERR_UNSUPPORTED_MIME_TYPE' throw error; } return imageMimeType; } /** * Generates an error payload to be returned with promise reject * -
chris-burkhardt renamed this gist
Jul 15, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
chris-burkhardt created this gist
Jun 10, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ 'use strict' const AWS = require("aws-sdk"); const S3 = new AWS.S3(); module.exports = async function (inputs, context) { // example encoded images can be created here: https://www.base64-image.de/ // upload image to S3 try { const s3BucketLocation = await uploadImageToS3(inputs.DeviceDiagnosticRequest.IMEIImage, "IMEIImage", inputs, context); } catch (error) { return Promise.reject(error); } return { Response: { Example: 'SomeValue' } }; } /** * Uploads a Base64 encoded string into an S3 bucket * * @param {string} imageData Base64 encoded image string * @param {string} imageName Name of image for S3 bucket key * @param {object} inputs Request object * @param {object} context * @returns The S3 bucker folder location as a string (does not include URL) */ async function uploadImageToS3(imageData, imageName, inputs, context) { const body = Buffer.from(imageData.split('base64')[1], 'base64'); const ts = new Date().getTime(); // timestamp labeled image name const s3ImageLocation = `folderName/${imageName.toLowerCase()}_${ts}.jpeg` const params = { Bucket: app.config.get('services:povo-service:diagnostics:S3Bucket'), Key: s3ImageLocation, Body: Buffer.from(body, 'base64'), ContentEncoding: 'base64', ContentType: 'image/jpeg' }; try { await S3.upload(params).promise(); return JSON.stringify(uploadOutput).body.Location; } catch (err) { throwError(`Could not upload image data to S3 error: ${err}`, inputs, context); } } /** * Generates an error payload to be returned with promise reject * * @param {string} message * @param {object} inputs * @param {object} context * @returns Object payload with error information */ function throwError(message, inputs, context) { const errorLog = { step: 'Diagnostics', text: message, payload: inputs, }; logger.logMessage( message, context, errorLog, null, 'Diagnostics' ); const error = new Error(message); error.status = 400; throw error; }