From 2643499e6591dd5f36a11a9de0f5da1049cb049f Mon Sep 17 00:00:00 2001 From: abhinav Date: Mon, 19 Dec 2022 15:52:00 +0000 Subject: [PATCH 1/6] Update 3 filesasd --- app.js | 47 ++++++++++++++++++++++++++++++++++++++++ package-lock.json | 55 ++++++++++++++++++++++++++++++++++++++++++++++- package.json | 3 ++- 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..7a5355c --- /dev/null +++ b/app.js @@ -0,0 +1,47 @@ +var express = require("express"); +const fileUpload = require("express-fileupload"); +const app = express(); + +app.use(fileUpload()); + +var AWS = require("aws-sdk"); + +app.post("/imageUpload", async (req, res) => { + AWS.config.update({ + accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Access key ID + secretAccesskey: process.env.AWS_SECRET_ACCESS_KEY, // Secret access key + region: "eu-west-1", //Region + }); + + const s3 = new AWS.S3(); + + // Binary data base64 + console.log(req.files); + + const fileContent = Buffer.from(req.files.uploadedFileName.data, "binary"); + // Setting up S3 upload parameters + const params = { + Bucket: "cyclic-nice-ruby-sea-urchin-garb-eu-west-1", + Key: "test.jpg", // File name you want to save as in S3 + Body: fileContent, + }; + + // Uploading files to the bucket + s3.upload(params, function (err, data) { + if (err) { + throw err; + } + res.send({ + response_code: 200, + response_message: "Success", + response_data: data, + }); + }); +}); + +const port = process.env.PORT || 3002 + + +app.listen(port, function () { + console.log("Example app listening on port 3000!"); +}); diff --git a/package-lock.json b/package-lock.json index a0e9fdf..0cb93c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,12 +5,14 @@ "requires": true, "packages": { "": { + "name": "starter-s3-storage", "version": "1.0.0", "license": "ISC", "dependencies": { "aws-sdk": "^2.1141.0", "body-parser": "^1.20.0", - "express": "^4.18.1" + "express": "^4.18.1", + "express-fileupload": "^1.4.0" }, "devDependencies": { "env-cmd": "^10.1.0", @@ -237,6 +239,17 @@ "isarray": "^1.0.0" } }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -692,6 +705,17 @@ "node": ">= 0.10.0" } }, + "node_modules/express-fileupload": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.4.0.tgz", + "integrity": "sha512-RjzLCHxkv3umDeZKeFeMg8w7qe0V09w3B7oGZprr/oO2H/ISCgNzuqzn7gV3HRWb37GjRk429CCpSLS2KNTqMQ==", + "dependencies": { + "busboy": "^1.6.0" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -1753,6 +1777,14 @@ "node": ">= 0.8" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -2251,6 +2283,14 @@ "isarray": "^1.0.0" } }, + "busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "requires": { + "streamsearch": "^1.1.0" + } + }, "bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -2596,6 +2636,14 @@ "vary": "~1.1.2" } }, + "express-fileupload": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.4.0.tgz", + "integrity": "sha512-RjzLCHxkv3umDeZKeFeMg8w7qe0V09w3B7oGZprr/oO2H/ISCgNzuqzn7gV3HRWb37GjRk429CCpSLS2KNTqMQ==", + "requires": { + "busboy": "^1.6.0" + } + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -3387,6 +3435,11 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==" }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" + }, "string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", diff --git a/package.json b/package.json index 56c1588..45d8c10 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "dependencies": { "aws-sdk": "^2.1141.0", "body-parser": "^1.20.0", - "express": "^4.18.1" + "express": "^4.18.1", + "express-fileupload": "^1.4.0" }, "devDependencies": { "env-cmd": "^10.1.0", From 6f54a4636d9357e442fe383d1058f4d352128a70 Mon Sep 17 00:00:00 2001 From: abhinav Date: Mon, 19 Dec 2022 16:16:46 +0000 Subject: [PATCH 2/6] Update index.js --- index.js | 113 +++++++++++++++++++++---------------------------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/index.js b/index.js index 0518340..55ae169 100644 --- a/index.js +++ b/index.js @@ -1,76 +1,47 @@ -const express = require('express') -const app = express() -const AWS = require("aws-sdk"); -const s3 = new AWS.S3() -const bodyParser = require('body-parser'); - -app.use(bodyParser.json()) - -// curl -i https://some-app.cyclic.app/myFile.txt -app.get('*', async (req,res) => { - let filename = req.path.slice(1) - - try { - let s3File = await s3.getObject({ - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', s3File.ContentType) - res.send(s3File.Body.toString()).end() - } catch (error) { - if (error.code === 'NoSuchKey') { - console.log(`No such key ${filename}`) - res.sendStatus(404).end() - } else { - console.log(error) - res.sendStatus(500).end() +var express = require("express"); +const fileUpload = require("express-fileupload"); +const app = express(); + +app.use(fileUpload()); + +var AWS = require("aws-sdk"); + +app.post("/imageUpload", async (req, res) => { + AWS.config.update({ + accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Access key ID + secretAccesskey: process.env.AWS_SECRET_ACCESS_KEY, // Secret access key + region: "eu-west-1", //Region + }); + + const s3 = new AWS.S3(); + + // Binary data base64 + console.log(req.files); + + const fileContent = Buffer.from(req.files.uploadedFileName.data, "binary"); + // Setting up S3 upload parameters + const params = { + Bucket: "cyclic-nice-ruby-sea-urchin-garb-eu-west-1", + Key: "test.jpg", // File name you want to save as in S3 + Body: fileContent, + }; + + // Uploading files to the bucket + s3.upload(params, function (err, data) { + if (err) { + throw err; } - } -}) - - -// curl -i -XPUT --data '{"k1":"value 1", "k2": "value 2"}' -H 'Content-type: application/json' https://some-app.cyclic.app/myFile.txt -app.put('*', async (req,res) => { - let filename = req.path.slice(1) - - console.log(typeof req.body) + res.send({ + response_code: 200, + response_message: "Success", + response_data: data, + }); + }); +}); - await s3.putObject({ - Body: JSON.stringify(req.body), - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', 'text/plain') - res.send('ok').end() -}) - -// curl -i -XDELETE https://some-app.cyclic.app/myFile.txt -app.delete('*', async (req,res) => { - let filename = req.path.slice(1) - - await s3.deleteObject({ - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', 'text/plain') - res.send('ok').end() -}) - -// ///////////////////////////////////////////////////////////////////////////// -// Catch all handler for all other request. -app.use('*', (req,res) => { - res.sendStatus(404).end() -}) - -// ///////////////////////////////////////////////////////////////////////////// -// Start the server const port = process.env.PORT || 3000 -app.listen(port, () => { - console.log(`index.js listening at http://localhost:${port}`) -}) - +app.listen(port, function () { + console.log("Example app listening on port 3000!"); +}); From 5e0a66498a8a36594420017534e0ec4282b26de6 Mon Sep 17 00:00:00 2001 From: abhinavdhangar <86644157+abhinavdhangar@users.noreply.github.com> Date: Mon, 19 Dec 2022 21:52:25 +0530 Subject: [PATCH 3/6] Update index.js --- index.js | 113 +++++++++++++++++++++---------------------------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/index.js b/index.js index 0518340..55ae169 100644 --- a/index.js +++ b/index.js @@ -1,76 +1,47 @@ -const express = require('express') -const app = express() -const AWS = require("aws-sdk"); -const s3 = new AWS.S3() -const bodyParser = require('body-parser'); - -app.use(bodyParser.json()) - -// curl -i https://some-app.cyclic.app/myFile.txt -app.get('*', async (req,res) => { - let filename = req.path.slice(1) - - try { - let s3File = await s3.getObject({ - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', s3File.ContentType) - res.send(s3File.Body.toString()).end() - } catch (error) { - if (error.code === 'NoSuchKey') { - console.log(`No such key ${filename}`) - res.sendStatus(404).end() - } else { - console.log(error) - res.sendStatus(500).end() +var express = require("express"); +const fileUpload = require("express-fileupload"); +const app = express(); + +app.use(fileUpload()); + +var AWS = require("aws-sdk"); + +app.post("/imageUpload", async (req, res) => { + AWS.config.update({ + accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Access key ID + secretAccesskey: process.env.AWS_SECRET_ACCESS_KEY, // Secret access key + region: "eu-west-1", //Region + }); + + const s3 = new AWS.S3(); + + // Binary data base64 + console.log(req.files); + + const fileContent = Buffer.from(req.files.uploadedFileName.data, "binary"); + // Setting up S3 upload parameters + const params = { + Bucket: "cyclic-nice-ruby-sea-urchin-garb-eu-west-1", + Key: "test.jpg", // File name you want to save as in S3 + Body: fileContent, + }; + + // Uploading files to the bucket + s3.upload(params, function (err, data) { + if (err) { + throw err; } - } -}) - - -// curl -i -XPUT --data '{"k1":"value 1", "k2": "value 2"}' -H 'Content-type: application/json' https://some-app.cyclic.app/myFile.txt -app.put('*', async (req,res) => { - let filename = req.path.slice(1) - - console.log(typeof req.body) + res.send({ + response_code: 200, + response_message: "Success", + response_data: data, + }); + }); +}); - await s3.putObject({ - Body: JSON.stringify(req.body), - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', 'text/plain') - res.send('ok').end() -}) - -// curl -i -XDELETE https://some-app.cyclic.app/myFile.txt -app.delete('*', async (req,res) => { - let filename = req.path.slice(1) - - await s3.deleteObject({ - Bucket: process.env.BUCKET, - Key: filename, - }).promise() - - res.set('Content-type', 'text/plain') - res.send('ok').end() -}) - -// ///////////////////////////////////////////////////////////////////////////// -// Catch all handler for all other request. -app.use('*', (req,res) => { - res.sendStatus(404).end() -}) - -// ///////////////////////////////////////////////////////////////////////////// -// Start the server const port = process.env.PORT || 3000 -app.listen(port, () => { - console.log(`index.js listening at http://localhost:${port}`) -}) - +app.listen(port, function () { + console.log("Example app listening on port 3000!"); +}); From 73d58983ae38e4b43cfd9cf5fee646e4af309195 Mon Sep 17 00:00:00 2001 From: abhinav Date: Mon, 19 Dec 2022 16:33:26 +0000 Subject: [PATCH 4/6] Update index.js --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 55ae169..d76f777 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,8 @@ app.post("/imageUpload", async (req, res) => { AWS.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Access key ID secretAccesskey: process.env.AWS_SECRET_ACCESS_KEY, // Secret access key - region: "eu-west-1", //Region + region: "eu-west-1", //Region, + }); const s3 = new AWS.S3(); @@ -21,6 +22,7 @@ app.post("/imageUpload", async (req, res) => { const fileContent = Buffer.from(req.files.uploadedFileName.data, "binary"); // Setting up S3 upload parameters const params = { + acl: 'public-read', Bucket: "cyclic-nice-ruby-sea-urchin-garb-eu-west-1", Key: "test.jpg", // File name you want to save as in S3 Body: fileContent, From b66f465c24fdba9923bb58fcf0758760a09e9f9c Mon Sep 17 00:00:00 2001 From: abhinavdhangar <86644157+abhinavdhangar@users.noreply.github.com> Date: Mon, 19 Dec 2022 22:05:36 +0530 Subject: [PATCH 5/6] Update index.js --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 55ae169..14b23c1 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ app.post("/imageUpload", async (req, res) => { Bucket: "cyclic-nice-ruby-sea-urchin-garb-eu-west-1", Key: "test.jpg", // File name you want to save as in S3 Body: fileContent, + acl: 'public-read' }; // Uploading files to the bucket From 0bc348883a4fc3064da88a0924b73495f7623e08 Mon Sep 17 00:00:00 2001 From: abhinav Date: Mon, 19 Dec 2022 17:18:09 +0000 Subject: [PATCH 6/6] md --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d76f777..37de516 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ app.post("/imageUpload", async (req, res) => { const s3 = new AWS.S3(); // Binary data base64 - console.log(req.files); + const fileContent = Buffer.from(req.files.uploadedFileName.data, "binary"); // Setting up S3 upload parameters