All endpoints working with AWS

This commit is contained in:
Duncan Phelps 2021-05-03 13:58:45 -05:00
parent 66524e5d79
commit 000024426b
10 changed files with 79 additions and 58 deletions

2
.gitignore vendored

@ -1,5 +1,7 @@
.now
node_modules
.env
.serverless
# Environment Variables
.env

@ -161,12 +161,12 @@
"S3Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"S3Key": "serverless/localstack-lambda/dev/1620056787473-2021-05-03T15:46:27.473Z/localstack-lambda.zip"
"S3Key": "serverless/localstack-lambda/dev/1620092918879-2021-05-04T01:48:38.879Z/localstack-lambda.zip"
},
"Handler": "api/server.handler",
"Runtime": "nodejs12.x",
"FunctionName": "localstack-lambda-dev-app",
"MemorySize": 128,
"MemorySize": 512,
"Timeout": 6,
"Role": {
"Fn::GetAtt": [
@ -179,14 +179,14 @@
"AppLogGroup"
]
},
"AppLambdaVersionpKt6OvdECa0w3L8STXgnFAZzVQXb1lv1UQDQyHZmwu8": {
"AppLambdaVersionuvewJZYompWGhR7PreRGORUZWWNhF3zGn5TtfgsWIs": {
"Type": "AWS::Lambda::Version",
"DeletionPolicy": "Retain",
"Properties": {
"FunctionName": {
"Ref": "AppLambdaFunction"
},
"CodeSha256": "ti9O4pTltT2kEw78OJXfrJVusyoy/3C+KWaYQByAA20="
"CodeSha256": "3b8ZgXd14AZAi3l2N89aVQ4LE9Ty39V8tHZhL6Q1t3w="
}
},
"ApiGatewayRestApi": {
@ -305,7 +305,7 @@
"MethodResponses": []
}
},
"ApiGatewayDeployment1620056783201": {
"ApiGatewayDeployment1620092914902": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
@ -368,7 +368,7 @@
"AppLambdaFunctionQualifiedArn": {
"Description": "Current Lambda function version",
"Value": {
"Ref": "AppLambdaVersionpKt6OvdECa0w3L8STXgnFAZzVQXb1lv1UQDQyHZmwu8"
"Ref": "AppLambdaVersionuvewJZYompWGhR7PreRGORUZWWNhF3zGn5TtfgsWIs"
},
"Export": {
"Name": "sls-localstack-lambda-dev-AppLambdaFunctionQualifiedArn"

Binary file not shown.

@ -9,7 +9,7 @@
"runtime": "nodejs12.x",
"stage": "dev",
"region": "us-east-1",
"memorySize": 128,
"memorySize": 512,
"variableSyntax": "\\${([^{}:]+?(?:\\(|:)(?:[^:{}][^{}]*?)?)}",
"versionFunctions": true,
"compiledCloudFormationTemplate": {
@ -175,12 +175,12 @@
"S3Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"S3Key": "serverless/localstack-lambda/dev/1620056787473-2021-05-03T15:46:27.473Z/localstack-lambda.zip"
"S3Key": "serverless/localstack-lambda/dev/1620092918879-2021-05-04T01:48:38.879Z/localstack-lambda.zip"
},
"Handler": "api/server.handler",
"Runtime": "nodejs12.x",
"FunctionName": "localstack-lambda-dev-app",
"MemorySize": 128,
"MemorySize": 512,
"Timeout": 6,
"Role": {
"Fn::GetAtt": [
@ -193,14 +193,14 @@
"AppLogGroup"
]
},
"AppLambdaVersionpKt6OvdECa0w3L8STXgnFAZzVQXb1lv1UQDQyHZmwu8": {
"AppLambdaVersionuvewJZYompWGhR7PreRGORUZWWNhF3zGn5TtfgsWIs": {
"Type": "AWS::Lambda::Version",
"DeletionPolicy": "Retain",
"Properties": {
"FunctionName": {
"Ref": "AppLambdaFunction"
},
"CodeSha256": "ti9O4pTltT2kEw78OJXfrJVusyoy/3C+KWaYQByAA20="
"CodeSha256": "3b8ZgXd14AZAi3l2N89aVQ4LE9Ty39V8tHZhL6Q1t3w="
}
},
"ApiGatewayRestApi": {
@ -319,7 +319,7 @@
"MethodResponses": []
}
},
"ApiGatewayDeployment1620056783201": {
"ApiGatewayDeployment1620092914902": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
@ -382,7 +382,7 @@
"AppLambdaFunctionQualifiedArn": {
"Description": "Current Lambda function version",
"Value": {
"Ref": "AppLambdaVersionpKt6OvdECa0w3L8STXgnFAZzVQXb1lv1UQDQyHZmwu8"
"Ref": "AppLambdaVersionuvewJZYompWGhR7PreRGORUZWWNhF3zGn5TtfgsWIs"
},
"Export": {
"Name": "sls-localstack-lambda-dev-AppLambdaFunctionQualifiedArn"
@ -514,11 +514,11 @@
],
"name": "localstack-lambda-dev-app",
"package": {},
"memory": 128,
"memory": 512,
"timeout": 6,
"runtime": "nodejs12.x",
"vpc": {},
"versionLogicalId": "AppLambdaVersionpKt6OvdECa0w3L8STXgnFAZzVQXb1lv1UQDQyHZmwu8"
"versionLogicalId": "AppLambdaVersionuvewJZYompWGhR7PreRGORUZWWNhF3zGn5TtfgsWIs"
}
},
"configValidationMode": "warn",
@ -540,7 +540,7 @@
"artifact": "/Users/duncanphelps/Documents/Learning/Javascript/sheetaki/sheetaki/.serverless/localstack-lambda.zip"
},
"package": {
"artifactDirectoryName": "serverless/localstack-lambda/dev/1620056787473-2021-05-03T15:46:27.473Z",
"artifactDirectoryName": "serverless/localstack-lambda/dev/1620092918879-2021-05-04T01:48:38.879Z",
"artifact": "localstack-lambda.zip"
}
}

@ -1,27 +1,13 @@
const URL = require('url');
const do_wb = require('../../src/util');
fs = require('fs');
const AWS = require('aws-sdk');
const { getFile } = require('../s3');
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 });
const s3 = new AWS.S3();
const getFile = (filename) => {
s3.getObject({ Bucket: 'sheetaki-test', Key: filename }, function (err, data) {
if (err) return res.status(500).send(err.message || err);
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);
// });
getFile(req, res, url, url.query.filename);
}
};

38
api/s3.js Normal file

@ -0,0 +1,38 @@
require('dotenv').config();
const S3 = require('aws-sdk/clients/s3');
const do_wb = require('../src/util');
const bucketName = process.env.AWS_BUCKET_NAME
const region = process.env.AWS_BUCKET_REGION
const secretAccessKey = process.env.AWS_SECRET_KEY
const accessKeyId = process.env.AWS_ACCESS_KEY
const s3 = new S3({
region,
accessKeyId,
secretAccessKey
})
function uploadFile(filename, file){
filename = filename.split("/");
filename = filename[filename.length - 1]
const uploadParams = {
Bucket: bucketName,
Key: filename,
Body: file
};
return s3.upload(uploadParams).promise()
}
exports.uploadFile = uploadFile;
function getFile(req, res, url, filename){
const params = {
Key: filename,
Bucket: bucketName,
}
s3.getObject(params, function (err, data) {
if (err) return res.status(500).send(err.message || err);
do_wb(req, data.Body, url, res);
});
}
exports.getFile = getFile;

@ -3,6 +3,7 @@ const do_wb = require('../../src/util');
const formidable = require('formidable-serverless');
const tmp = require('tmp');
const AWS = require('aws-sdk');
const { uploadFile } = require('../s3');
module.exports = function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
@ -19,28 +20,6 @@ module.exports = function (req, res) {
return tmpobj.name;
}
// 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: "sheetaki-test",
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);
});
};
//
form.parse(req, (err, fields, files) => {
if (err) return res.status(400).send(err.message || err);
if (!url.query) url.query = fields;
@ -54,11 +33,12 @@ module.exports = function (req, res) {
/* read file */
const file = fentries[0][1];
fs.readFile(file.path, (err, body) => {
fs.readFile(file.path, async (err, body) => {
if (err) return res.status(500).send(err.message || err);
const tmpFile = newFile();
uploadFile(tmpFile, body);
//writeFile(tmpFile, body);
const result = await uploadFile(tmpFile, body);
console.log(result);
res.send(result.key);
});
});
};

14
package-lock.json generated

@ -10,6 +10,7 @@
"dependencies": {
"aws-sdk": "^2.896.0",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"formidable-serverless": "",
"request": "",
@ -373,6 +374,14 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"node_modules/dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==",
"engines": {
"node": ">=8"
}
},
"node_modules/ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
@ -1596,6 +1605,11 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
},
"ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",

@ -13,6 +13,7 @@
"dependencies": {
"aws-sdk": "^2.896.0",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"formidable-serverless": "",
"request": "",

@ -19,7 +19,7 @@ provider:
runtime: nodejs12.x
stage: dev
region: us-east-1
memorySize: 128
memorySize: 512
functions:
app:
handler: api/server.handler