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

Update 3 filesasd #1

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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!");
});
102 changes: 38 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,50 @@
const express = require('express')
const app = express()
const AWS = require("aws-sdk");
const s3 = new AWS.S3()
const bodyParser = require('body-parser');
var express = require("express");
const fileUpload = require("express-fileupload");
const app = express();

app.use(bodyParser.json())
app.use(fileUpload());

// curl -i https://some-app.cyclic.app/myFile.txt
app.get('*', async (req,res) => {
let filename = req.path.slice(1)
var AWS = require("aws-sdk");

try {
let s3File = await s3.getObject({
Bucket: process.env.BUCKET,
Key: filename,
}).promise()
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,

});

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()
}
}
})


// 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)
const s3 = new AWS.S3();

await s3.putObject({
Body: JSON.stringify(req.body),
Bucket: process.env.BUCKET,
Key: filename,
}).promise()
// Binary data base64

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)
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,
acl: 'public-read'
};

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()
})
// 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,
});
});
});

// /////////////////////////////////////////////////////////////////////////////
// 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!");
});
55 changes: 54 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down