/save and /file endpoints working with localstack s3 bucket
This commit is contained in:
parent
ace3670339
commit
876cd0d84f
@ -1,16 +1,26 @@
|
||||
const URL = require('url');
|
||||
const do_wb = require('../../src/util');
|
||||
fs = require('fs');
|
||||
const AWS = require('aws-sdk');
|
||||
|
||||
module.exports = function (req, res) {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
const url = URL.parse(req.url, true);
|
||||
|
||||
if (!url.query.filename) return res.status(400).send("Must specify filename");
|
||||
if (url.query.filename){
|
||||
fs.readFile(url.query.filename, (err, body) => {
|
||||
const s3 = new AWS.S3({ endpoint: 'http://localhost:4566', s3ForcePathStyle: true });
|
||||
|
||||
const getFile = (filename) => {
|
||||
s3.getObject({ Bucket: 'localstacktest', Key: filename }, function (err, data) {
|
||||
if (err) return res.status(500).send(err.message || err);
|
||||
do_wb(req, body, url, res);
|
||||
do_wb(req, data.Body, url, res);
|
||||
});
|
||||
}
|
||||
|
||||
if (!url.query.filename) return res.status(400).send("Must specify filename");
|
||||
if (url.query.filename){
|
||||
getFile(url.query.filename);
|
||||
// fs.readFile(url.query.filename, (err, body) => {
|
||||
// if (err) return res.status(500).send(err.message || err);
|
||||
// do_wb(req, body, url, res);
|
||||
// });
|
||||
}
|
||||
};
|
@ -2,10 +2,12 @@ const URL = require('url'), fs = require('fs');
|
||||
const do_wb = require('../../src/util');
|
||||
const formidable = require('formidable-serverless');
|
||||
const tmp = require('tmp');
|
||||
const AWS = require('aws-sdk');
|
||||
|
||||
module.exports = function (req, res) {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
const url = URL.parse(req.url, true);
|
||||
const s3 = new AWS.S3({ endpoint: 'http://localhost:4566', s3ForcePathStyle: true });
|
||||
|
||||
/* parse form data */
|
||||
const form = formidable({ multiples: true, maxFileSize: 2 * 1024 * 1024 });
|
||||
@ -16,12 +18,28 @@ module.exports = function (req, res) {
|
||||
return tmpobj.name;
|
||||
}
|
||||
|
||||
const writeFile = (fileName, file) => {
|
||||
fs.writeFile(fileName, file, (err) => {
|
||||
// const writeFile = (filename, file) => {
|
||||
// fs.writeFile(filename, file, (err) => {
|
||||
// if (err) return res.status(500).send(err.message || err);
|
||||
// res.status(201).send(filename);
|
||||
// });
|
||||
// }
|
||||
|
||||
//upload file to s3 bucket
|
||||
const uploadFile = (filename, file) => {
|
||||
filename = filename.split("/");
|
||||
filename = filename[filename.length-1]
|
||||
const params = {
|
||||
Bucket: "localstacktest",
|
||||
Key: filename,
|
||||
Body: file
|
||||
};
|
||||
s3.putObject(params, function (err, data) {
|
||||
if (err) return res.status(500).send(err.message || err);
|
||||
res.status(201).send(fileName);
|
||||
res.status(201).send(filename);
|
||||
});
|
||||
}
|
||||
};
|
||||
//
|
||||
form.parse(req, (err, fields, files) => {
|
||||
if (err) return res.status(400).send(err.message || err);
|
||||
if (!url.query) url.query = fields;
|
||||
@ -38,7 +56,8 @@ module.exports = function (req, res) {
|
||||
fs.readFile(file.path, (err, body) => {
|
||||
if (err) return res.status(500).send(err.message || err);
|
||||
const tmpFile = newFile();
|
||||
writeFile(tmpFile, body);
|
||||
uploadFile(tmpFile, body);
|
||||
//writeFile(tmpFile, body);
|
||||
});
|
||||
});
|
||||
};
|
1378
package-lock.json
generated
1378
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -11,6 +11,7 @@
|
||||
"excel"
|
||||
],
|
||||
"dependencies": {
|
||||
"aws-sdk": "^2.896.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"express": "^4.17.1",
|
||||
"formidable-serverless": "",
|
||||
|
Loading…
Reference in New Issue
Block a user